Get the substring of the column in Pandas-Python Last Updated : 10 Jul, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Example 1: We can loop through the range of the column and calculate the substring for each value in the column. Python3 1== # importing pandas as pd import pandas as pd # creating a dictionary dict = {'Name':["John Smith", "Mark Wellington", "Rosie Bates", "Emily Edward"]} # converting the dictionary to a # dataframe df = pd.DataFrame.from_dict(dict) # storing first 3 letters of name for i in range(0, len(df)): df.iloc[i].Name = df.iloc[i].Name[:3] df Output: Note: For more information, refer Python Extracting Rows Using Pandas Example 2: In this example we'll use str.slice(). Python3 1== # importing pandas as pd import pandas as pd # creating a dictionary dict = {'Name':["John Smith", "Mark Wellington", "Rosie Bates", "Emily Edward"]} # converting the dictionary to a # dataframe df = pd.DataFrame.from_dict(dict) # storing first 3 letters of name as username df['UserName'] = df['Name'].str.slice(0, 3) df Output: Example 3: We can also use the str accessor in a different way by using square brackets. Python3 1== # importing pandas as pd import pandas as pd # creating a dictionary dict = {'Name':["John Smith", "Mark Wellington", "Rosie Bates", "Emily Edward"]} # converting the dictionary to a dataframe df = pd.DataFrame.from_dict(dict) # storing first 3 letters of name as username df['UserName'] = df['Name'].str[:3] df Output: 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. Python3 1== # importing pandas as pd import pandas as pd # creating a dictionary dict = {'Name':["John Smith", "Mark Wellington", "Rosie Bates", "Emily Edward"]} # converting the dictionary to a dataframe df = pd.DataFrame.from_dict(dict) # storing lastname of each person df['LastName'] = df.Name.str.extract(r'\b(\w+)$', expand = True) df Output: Comment More infoAdvertise with us Next Article Get the data type of column in Pandas - Python P parasmadan15 Follow Improve Article Tags : Python Python-pandas Python pandas-dataFrame Practice Tags : python Similar Reads Get the data type of column in Pandas - Python Letâs see how to get data types of columns in the pandas dataframe. First, Letâs create a pandas dataframe. Example: Python3 # importing pandas library import pandas as pd # List of Tuples employees = [ ('Stuti', 28, 'Varanasi', 20000), ('Saumya', 32, 'Delhi', 25000), ('Aaditya', 25, 'Mumbai', 40000 3 min read Python | pandas int to string with leading zeros In Python, When it comes to data manipulation and data analysis, Panda is a powerful library that provides capabilities to easily encounter scenarios where the conversion of integer to string with leading zeros is needed. In this article, we will discuss How we can convert pandas int to string with 4 min read Python | Pandas Index.get_slice_bound() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.get_slice_bound() function calculate slice bound that corresponds to give 2 min read Python | Pandas Series.str.get_dummies() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.get_dummies() is used to separate each string in the caller series at the p 3 min read Python | Pandas series.str.get() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.get() method is used to get element at the passed position. This method wor 3 min read Get column names from CSV using Python CSV (Comma Separated Values) files store tabular data as plain text, with values separated by commas. They are widely used in data analysis, machine learning and statistical modeling. In Python, you can work with CSV files using built-in libraries like csv or higher-level libraries like pandas. In t 2 min read Like