Just a quick question guys, I have a pandas dataframe:
In [11]: df = pd.DataFrame([['A', 'B'], ['C', E], ['D', 'C']],columns=['X', 'Y', 'Z'])
In [12]: df
Out[12]:
X Y Z
0 A B D
1 C E C
How can I convert to lower all the elements of df
:
Out[12]:
X Y Z
0 a b d
1 c e c
I look over the documentation and I tried the following:
df = [[col.lower() for col in [df["X"],df["Y"], df["Z"]]]]
df
Nevertheless, it doesnt work. How to lower all the elements inside a pandas dataframe?.
Either
df.applymap(str.lower)
Out:
X Y Z
0 a b d
1 c e c
Or
df.apply(lambda col: col.str.lower())
Out:
X Y Z
0 a b d
1 c e c
The first one is faster and it looks nicer but the second one can handle NaNs.
using applymap with lambda will work even if df contain NaN values and String
import pandas as pd
df = df.applymap(lambda x: x.lower() if pd.notnull(x) else x)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.