简体   繁体   English

通过列表理解将str.lower应用于熊猫

[英]Apply str.lower to Pandas via list Comprehension

I have a dataframe df of the form: 我有以下形式的数据框df:

    animal    fruit
0    "Dog"    "Apple"
1    "Cat"    "Banana"
2    "Rat"    "Grape"

I want to apply str.lower() to all columns (but not headers). 我想将str.lower()应用于所有列(而不是标题)。

This Works: 这有效:

for i in df:
    df[i] = df[i].str.lower()

How can I write this as a list comphrension? 如何将其编写为列表功能?

I tried: 我试过了:

df[i] = [df[i].str.lower() for i in df]

But this does not work and I get a: 但这不起作用,我得到:

TypeError: list indices must be integers, not instancemethod

What must I change within the list comprehension for this to work? 我必须在清单理解中进行哪些更改才能使其正常工作?

Secondly, is there a more "Pandas-onicy" way of doing this in general, perhaps using the pandas.apply() function? 其次,一般是否可以使用pandas.apply()函数来实现“熊猫似的”?

Many thanks for your help. 非常感谢您的帮助。

Output from list comprehension is list of Series . 列表推导的输出是Series列表。 So need concat list : 因此需要concat list

L = [df[i].str.lower() for i in df]
print (L)
[0    dog
1    cat
2    rat
Name: animal, dtype: object, 0     apple
1    banana
2     grape
Name: fruit, dtype: object]

df1 = pd.concat(L, axis=1)
print (df1)
  animal   fruit
0    dog   apple
1    cat  banana
2    rat   grape

Solution with apply : apply解决方案:

print (df.apply(lambda x: x.str.lower()))
  animal   fruit
0    dog   apple
1    cat  banana
2    rat   grape

Timings : 时间

df = pd.concat([df]*1000).reset_index(drop=True)
df = pd.concat([df]*1000, axis=1)
df.columns = range(len(df.columns))
#[3000 rows x 2000 columns]
print (df)

In [89]: %timeit (pd.concat([df[i].str.lower() for i in df], axis=1))
1 loop, best of 3: 2.3 s per loop

In [90]: %timeit (df.apply(lambda x: x.str.lower()))
1 loop, best of 3: 2.63 s per loop

In [91]: %timeit (df.stack().str.lower().unstack())
1 loop, best of 3: 5.04 s per loop

You can stack so that it makes a single column, then call str.lower , and then unstack to restore the columns back: 您可以stack以使其成为单个列,然后调用str.lower ,然后str.lower unstack以将列还原回:

In [74]:
df = df.stack().str.lower().unstack()
df

Out[74]:
  animal   fruit
0    dog   apple
1    cat  banana
2    rat   grape

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM