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
- Get column index from column name of a given Pandas DataFrame
- Delete a column from a Pandas DataFrame
- Add column names to dataframe in Pandas
- How to add Empty Column to Dataframe in Pandas?
- How to get column names in Pandas dataframe
- Get a list of a specified column of a Pandas DataFrame
- Get a list of a particular column values of a Pandas DataFrame
- How to lowercase column names in Pandas dataframe
- Get unique values from a column in Pandas DataFrame
- Getting Unique values from a column in Pandas dataframe
- Replace all the NaN values with Zero's in a column of a Pandas dataframe
- How to Convert Index to Column in Pandas Dataframe?
- How to Concatenate Column Values in Pandas DataFrame?
- Apply uppercase to a column in Pandas dataframe
- Formatting integer column of Dataframe in Pandas
- Check whether a given column is present in a Pandas DataFrame or not
- Capitalize first letter of a column in Pandas dataframe
- Adding new column to existing DataFrame in Pandas
- Create a column using for loop in Pandas Dataframe
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.