Apply String Methods Across Multiple Columns in a Pandas DataFrame Last Updated : 23 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a dataframe in Pandas with multiple columns, and we want to apply string methods to transform the data within these columns. In this article, we will explore three different approaches to applying string methods to multiple columns of a dataframe. Apply String Methods to Multiple Columns of a DataframeBelow are the possible approaches to apply string methods to multiple columns of a dataframe in Python: Using applymapUsing apply with lambdaUsing assign with str accessorApply String Methods To Multiple Columns Of A Dataframe Using applymapIn this example, we are using the applymap function to apply the str.lower method to all elements in the specified columns ('name' and 'city'). This converts all string values in these columns to lowercase. Python import pandas as pd df = pd.DataFrame({ 'name': ['GeeksForGeeks', 'CodingForAll', 'CodeWars'], 'city': ['Noida', 'San Francisco', 'Los Angeles'] }) df[['name', 'city']] = df[['name', 'city']].applymap(lambda x: x.lower()) print(df) Output name city 0 geeksforgeeks noida 1 codingforall san francisco 2 codewars los angeles Apply String Methods To Multiple Columns Of A Dataframe Using apply with lambdaIn this example, we are using the apply method with a lambda function to apply the str.upper method to each element in the 'name' and 'city' columns. This converts all string values in these columns to uppercase. Python import pandas as pd df = pd.DataFrame({ 'name': ['GeeksForGeeks', 'CodingForAll', 'CodeWars'], 'city': ['Noida', 'San Francisco', 'Los Angeles'] }) df['name'] = df['name'].apply(lambda x: x.upper()) df['city'] = df['city'].apply(lambda x: x.upper()) print(df) Output name city 0 GEEKSFORGEEKS NOIDA 1 CODINGFORALL SAN FRANCISCO 2 CODEWARS LOS ANGELES Apply String Methods To Multiple Columns Of A Dataframe Using assign with str accessorIn this example, we are using the assign method with the str accessor in pandas to apply the capitalize string method to the 'name' and 'city' columns of the dataframe, converting the first letter of each word to uppercase. Python import pandas as pd df = pd.DataFrame({ 'name': ['geeksforgeeks', 'codingforall', 'codewars'], 'city': ['noida', 'san francisco', 'los angeles'] }) df = df.assign(name=df['name'].str.capitalize(), city=df['city'].str.capitalize()) print(df) Output name city 0 Geeksforgeeks Noida 1 Codingforall San francisco 2 Codewars Los angeles Comment More infoAdvertise with us Next Article Apply String Methods Across Multiple Columns in a Pandas DataFrame G gauravggeeksforgeeks Follow Improve Article Tags : Pandas Python pandas-dataFrame pandas-dataframe-program Similar Reads Return multiple columns using Pandas apply() method Objects passed to the pandas.apply() are Series objects whose index is either the DataFrameâs index (axis=0) or the DataFrameâs columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type ar 3 min read Add multiple columns to dataframe in Pandas In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe. Add multiple columns to a DataFrame using ListsPython3 # importing pandas library import pandas as pd # creating and initializing a nested list students = [[' 3 min read How to select multiple columns in a pandas dataframe 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. In this article, we will discuss all the different ways of selecting multiple columns 5 min read How to Apply a function to multiple columns in Pandas? Pandas.apply allow the users to pass a function and apply it on every single value of the Pandas series. Syntax of pandas.DataFrame.apply Syntax : DataFrame.apply(parameters) Parameters : func : Function to apply to each column or row.axis : Axis along which the function is appliedraw : Determines 3 min read How to Access a Column in a DataFrame with Pandas In this article we will explore various techniques to access a column in a dataframe with pandas with concise explanations and practical examples.Method 1: Accessing a Single Column Using Bracket NotationBracket notation is the most straightforward method to access a column. Use the syntax df['colum 4 min read Like