I was able to do this in the DataFrame using a lambda function with map(lambda x: x.lower()). I tried to use a lambda function with pd.series.apply() but that didn't work. Also when I try to isolate the column in series with something like series['A'] should it return the index(although I guess this makes sense) because I get a float error even though the values that I want to apply the lower method to are strings. Any help would be appreciated.
Asked
Active
Viewed 3,733 times
4
-
I'm not sure what you tried which was working for the entire DataFrame, perhaps `df.applymap(lambda x: x.lower() if isinstance(x, basestring) else x)` (Note: this is not very efficient). – Andy Hayden Nov 08 '13 at 18:55
1 Answers
3
You can use the Series vectorised string methods, which includes lower:
In [11]: df = pd.DataFrame([['A', 'B'], ['C', 4]], columns=['X', 'Y'])
In [12]: df
Out[12]:
X Y
0 A B
1 C 4
In [13]: df.X.str.lower()
Out[13]:
0 a
1 c
Name: X, dtype: object
In [14]: df.Y.str.lower()
Out[14]:
0 b
1 NaN
Name: Y, dtype: object

Andy Hayden
- 359,921
- 101
- 625
- 535
-
Thanks. That is more concise than what I was using. I think I also found another problem. I was reading a csv with pd.read_csv. There were gaps, which read_csv filled with nan's which are floats and not strings, so I think that is why I got the error. – user2592989 Nov 08 '13 at 19:01
-
@user2592989 NaN is the placeholder for missing data used throughout pandas http://stackoverflow.com/a/17534682/1240268 (it's a float, so that explains it) – Andy Hayden Nov 08 '13 at 19:10
-
1