str.lower()
does not modify the existing column. It just returns a new Series with the lowercase transform applied. If you want to overwrite the original column, you need to assign the result back to it:
df['sex'] = df.sex.str.lower()
Next, change the strings to lowercase using this template:,You may use the following syntax to change strings to lowercase in Pandas DataFrame:,You may also want to check the following guide for the steps to change strings to uppercase in Pandas DataFrame.,In that case, the logic to change the strings to lowercase is the same:
You may use the following syntax to change strings to lowercase in Pandas DataFrame:
df['column name'].str.lower()
To begin, let’s create a simple DataFrame with 5 fruits (all in uppercase) and their prices:
import pandas as pd
data = {
'Fruits': ['BANANA', 'APPLE', 'MANGO', 'WATERMELON', 'PEAR'],
'Price': [0.5, 1, 1.5, 2.5, 1]
}
df = pd.DataFrame(data, columns = ['Fruits', 'Price'])
print(df)
As you can see, all the 5 fruits are captured in uppercase:
Fruits Price 0 BANANA 0.5 1 APPLE 1.0 2 MANGO 1.5 3 WATERMELON 2.5 4 PEAR 1.0
So the complete Python code would look as follows:
import pandas as pd
data = {
'Fruits': ['BANANA', 'APPLE', 'MANGO', 'WATERMELON', 'PEAR'],
'Price': [0.5, 1, 1.5, 2.5, 1]
}
df = pd.DataFrame(data, columns = ['Fruits', 'Price'])
df['Fruits'] = df['Fruits'].str.lower()
print(df)
Run the code, and you’ll notice that the 5 fruits are now in lower case:
Fruits Price 0 banana 0.5 1 apple 1.0 2 mango 1.5 3 watermelon 2.5 4 pear 1.0
Convert strings in the Series/Index to lowercase.,Converts uppercase to lowercase and lowercase to uppercase.,Converts first character of each word to uppercase and remaining to lowercase.,Equivalent to str.lower().
>>> s = pd.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe']) >>>
s
0 lower
1 CAPITALS
2 this is a sentence
3 SwApCaSe
dtype: object
>>> s.str.lower()
0 lower
1 capitals
2 this is a sentence
3 swapcase
dtype: object
>>> s.str.upper()
0 LOWER
1 CAPITALS
2 THIS IS A SENTENCE
3 SWAPCASE
dtype: object
>>> s.str.title()
0 Lower
1 Capitals
2 This Is A Sentence
3 Swapcase
dtype: object
>>> s.str.capitalize()
0 Lower
1 Capitals
2 This is a sentence
3 Swapcase
dtype: object
>>> s.str.swapcase()
0 LOWER
1 capitals
2 THIS IS A SENTENCE
3 sWaPcAsE
dtype: object
Approach: Call the str.lower() function upon the column to change its string values to lowercase. To select a column, use the square bracket notation and specify the column name within it, for example, df['column_name'].,Thus, in this tutorial, we learned three different ways of converting the string value in a specific column of a DataFrame to lowercase. Please subscribe and stay tuned for more interesting solutions and discussions. ,Let’s dive into the different approaches that will help us to convert the upper case strings in the DataFrame to lowercase.,Use the map function upon this lambda function to apply the operation on each value in the selected column of the dataframe.
Example: Consider the following Pandas DataFrame
:
import pandas as pd
import numpy as np
data = {
'col_1': ['ONE', 'TWO', 'Three', np.NAN, '100'],
}
df = pd.DataFrame(data)
print(df)
Output:
col_1 0 ONE 1 TWO 2 Three 3 NaN 4 100
Expected Output:
col_1 0 one 1 two 2 three 3 NaN 4 100
Code:
import pandas as pd
import numpy as np
data = {
'col_1': ['ONE', 'TWO', 'Three', np.NAN, '100'],
}
df = pd.DataFrame(data)
df['col_1'] = df['col_1'].str.casefold()
print(df)
Example:
text = 'außen'
print(text.casefold())
text = 'außen'
print(text.lower())
where, df is the input dataframe and column_name is the name of the dataframe column, whose values need to be converted into lowercase.,This article will discuss different ways to convert all values of a Pandas Dataframe column to lowercase in Python.,In this article, we learn about three different ways to convert column values to lowercase in a Pandas dataframe.,Example: In this example, we are going to convert values of ‘Name’ and ‘ Subjects ‘ columns values into lowercase.
A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Let’s create a dataframe with five rows and four columns,
import pandas as pd # create dataframe with 5 rows and 4 columns df = pd.DataFrame({ 'Roll_Number': [11, 12, 13, 14, 15], 'Name': ['MARK', 'JOHN', 'RITIKA', 'SRAVAN', 'HARSHA'], 'Age': [21, 23, 22, 21, 23], 'Subjects': ['PHP', 'JAVA', 'CPP', 'PYTHON', 'HTML'] }) # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects
0 11 MARK 21 PHP
1 12 JOHN 23 JAVA
2 13 RITIKA 22 CPP
3 14 SRAVAN 21 PYTHON
4 15 HARSHA 23 HTML
df['column_name'].str.lower()
Now to convert all values in selected column (series), pass the str.lower() function as argument to the apply() function. It will convert all values in column to lower case. Checkout the complete example as follows,
import pandas as pd # create dataframe with 5 rows and 4 columns df = pd.DataFrame({ 'Roll_Number': [11, 12, 13, 14, 15], 'Name': ['MARK', 'JOHN', 'RITIKA', 'SRAVAN', 'HARSHA'], 'Age': [21, 23, 22, 21, 23], 'Subjects': ['PHP', 'JAVA', 'CPP', 'PYTHON', 'HTML'] }) # Display the Dataframe print(df) # Convert the value of 'Name' column to lowercase df['Name'] = df['Name'].apply(str.lower) # Convert the value of 'Subjects' column to lowercase df['Subjects'] = df['Subjects'].apply(str.lower) # Display the Dataframe print(df)
Now to convert all values in selected column (series), pass the str.lower() function as argument to the map() function. It will convert all values in the column to lower case. Check out the complete example as follows,
import pandas as pd # create dataframe with 5 rows and 4 columns df = pd.DataFrame({ 'Roll_Number': [11, 12, 13, 14, 15], 'Name': ['MARK', 'JOHN', 'RITIKA', 'SRAVAN', 'HARSHA'], 'Age': [21, 23, 22, 21, 23], 'Subjects': ['PHP', 'JAVA', 'CPP', 'PYTHON', 'HTML'] }) # Display the Dataframe print(df) # Convert the value of 'Name' column to lowercase df['Name'] = df['Name'].map(str.lower) # Convert the value of 'Subjects' column to lowercase df['Subjects'] = df['Subjects'].map(str.lower) # Display the Dataframe print(df)
str.lower() does not modify the existing column. It just returns a new Series with the lowercase transform applied. If you want to overwrite the original column, you need to assign the result back to it:,Checking if the values from the pandas dataframe column exist in another column. isin method not working,Can I use a value stored in a variable after an if statement with the loc method?,Pandas explode function not working for list of string column
str.lower()
does not modify the existing column. It just returns a new Series with the lowercase transform applied. If you want to overwrite the original column, you need to assign the result back to it:
df['sex'] = df.sex.str.lower()
Last Updated : 17 Sep, 2018
Python has some inbuilt methods to convert a string into lower, upper or Camel case. But these methods don’t work on list and other multi string objects. Pandas is a library for Data analysis which provides separate methods to convert all values in a series to respective text cases. Since, lower, upper and title are Python keywords too, .str has to be prefixed before calling these function on a Pandas series.
Syntax:
Series.str.lower() Series.str.upper() Series.str.title()
July 16, 2020 by cmdline
Let us load Pandas and scipy.stats.
import pandas as pd
from scipy.stats
import poisson
We will create a toy dataframe with three columns. We will first name the dataframe’s columns with upper cases.
c1 = poisson.rvs(mu = 10, size = 5)
c2 = poisson.rvs(mu = 15, size = 5)
c3 = poisson.rvs(mu = 20, size = 5)
df = pd.DataFrame({
"COLUMN1": c1,
"COLUMN2": c2,
"COLUMN3": c3
})
Our data frame’s column names starts with uppercase.
df.head() COLUMN1 COLUMN2 COLUMN3 0 16 12 16 1 12 14 11 2 15 15 23 3 8 14 24 4 11 15 32
In addition to upper cases, sometimes column names can have both leading and trailing empty spaces. Let us create a toy dataframe with column names having trailing spaces.
df = pd.DataFrame({
" C1 ": c1,
"C2": c2,
"C3 ": c3
})
By inspecting column names we can see the spaces.
df.columns
Index([' C1 ', 'C2', 'C3 '], dtype = 'object')