When a part of any column in Dataframe is important and the need is to take it separate, we can split a column on the basis of the requirement.
We can use Pandas .str accessor, it does fast vectorized string operations for Series and Dataframes and returns a string object. Pandas str accessor has number of useful methods and one of them is
str.split, it can be used with split to get the desired part of the string. To get the nth part of the string, first split the column by delimiter and apply str[n-1] again on the object returned, i.e.
Let’s make it clear by examples.
Code #1: Print a data object of the splitted column.
0 Geek1 1 Geek2 2 Geek3 3 Geek4 4 Geek5 dtype: object
Code #2: Print a list of returned data object.
['Geek1', 'Geek2', 'Geek3', 'Geek4', 'Geek5']
Code #3: Print a list of elements.
['id', 'id', 'id', 'id', 'id']
- Split a text column into two columns in Pandas DataFrame
- How to get column names in Pandas dataframe
- How to lowercase column names in Pandas dataframe
- Get n-smallest values from a particular column in Pandas DataFrame
- Formatting integer column of Dataframe in Pandas
- Getting Unique values from a column in Pandas dataframe
- Adding new column to existing DataFrame in Pandas
- Get unique values from a column in Pandas DataFrame
- Capitalize first letter of a column in Pandas dataframe
- Create a column using for loop in Pandas Dataframe
- Apply uppercase to a column in Pandas dataframe
- Get n-largest values from a particular column in Pandas DataFrame
- Split a String into columns using regex in pandas DataFrame
- Python | Creating a Pandas dataframe column based on a given condition
- Create a new column in Pandas DataFrame based on the existing columns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.