Now, we’ll see how we can get the substring for all the values of a column in a Pandas dataframe. This extraction can be very useful when working with data. For example, we have the first name and last name of different people in a column and we need to extract the first 3 letters of their name to create their username.
We can loop through the range of the column and calculate the substring for each value in the column.
Note: For more information, refer Python Extracting Rows Using Pandas
Example 2: In this example we’ll use
Example 3: We can also use the str accessor in a different way by using square brackets.
Example 4: We can also use str.extract for this task. In this example we’ll store last name of each person in “LastName” column.
- Get column index from column name of a given Pandas DataFrame
- Create a DataFrame from a Numpy array and specify the index column and column headers
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- How to get column names in Pandas dataframe
- Get unique values from a column in Pandas DataFrame
- Get the index of minimum value in DataFrame column
- Get the index of maximum value in DataFrame column
- Get n-smallest values from a particular column in Pandas DataFrame
- Get n-largest values from a particular column in Pandas DataFrame
- Split a column in Pandas dataframe and get part of it
- How to get column and row names in DataFrame?
- Get a list of a particular column values of a Pandas DataFrame
- Get the data type of column in Pandas - Python
- Get a list of a specified column of a Pandas DataFrame
- Get list of column headers from a Pandas DataFrame
- Pandas - GroupBy One Column and Get Mean, Min, and Max values
- Get all rows in a Pandas DataFrame containing given substring
- Capitalize first letter of a column in Pandas dataframe
- Python | Change column names and row indexes in Pandas DataFrame
- Convert the column type from string to datetime format 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.