![]() |
VOOZH | about |
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.
Below are the possible approaches to apply string methods to multiple columns of a dataframe in Python:
In 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.
name city 0 geeksforgeeks noida 1 codingforall san francisco 2 codewars los angeles
In 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.
name city 0 GEEKSFORGEEKS NOIDA 1 CODINGFORALL SAN FRANCISCO 2 CODEWARS LOS ANGELES
In 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.
name city 0 Geeksforgeeks Noida 1 Codingforall San francisco 2 Codewars Los angeles