john doe - 11 months ago 77

Python Question

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?.

Answer Source

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.