我有一个pandas数据框,我更喜欢使用lambda函数而不是循环来解决我的问题。
问题是这样的;
df = pd.DataFrame({'my_fruits':['fruit', 'fruit', 'fruit', 'fruit', 'fruit'],
'fruit_a': ['apple', 'banana', 'vegetable', 'vegetable', 'cherry'],
'fruit_b': ['vegetable', 'apple', 'vegeatble', 'pineapple', 'pear']})
如果我应用以下循环;
for i in np.arange(0,len(df)):
if df['fruit_a'][i] == 'vegetable' or df['fruit_b'][i] == 'vegetable':
df['my_fruits'][i] = 'not_fruit'
我能够得到我想要的结果。这是因为如果fruit_a
或fruit_b
列包含值vegetable
,我希望my_fruits
列等于not_fruit
。
我怎样才能在lamda函数中设置它。无法理解两列输入如何用于更改不同的列值。谢谢!
答案 0 :(得分:3)
您可以.live
使用Series.mask
:
boolean mask
mask = (df['fruit_a'] == 'vegetable') | (df['fruit_b'] == 'vegetable')
print (mask)
0 True
1 False
2 True
3 True
4 False
dtype: bool
df.my_fruits = df.my_fruits.mask(mask, 'not_fruits')
print (df)
fruit_a fruit_b my_fruits
0 apple vegetable not_fruits
1 banana apple fruit
2 vegetable vegetable not_fruits
3 vegetable pineapple not_fruits
4 cherry pear fruit
的另一个解决方案是按mask
比较所有选定的列,然后any
至少在一列中获取所有vegetable
:
True
答案 1 :(得分:2)
使用pd.Series.where
并在一步中检查'vegetable'
是否与any
相结合
where
与mask
相反,这就是我使用cond
的否定的原因
否则,这与jezrael的回答非常相似
cond = df[['fruit_a', 'fruit_b']].eq('vegetable').any(1)
df.my_fruits = df.my_fruits.where(~cond, 'not_fruit')
从我的手机回答。请原谅错别字。
答案 2 :(得分:1)
您可以使用apply
方法执行此操作:
https://api.coursera.org/api/catalog.v1/courses?fields=smallIcon%2CshortDescription&q=search&query=python
Got Positive response!
#<HTTParty::Response:0x2660970 parsed_response={"elements"=>[{"id"=>119, "shortName"=>"scicomp", "name"=>"High Performance Scientific Computing", "shortDescription"=>"Programming-oriented course on effectively using modern computers to solve scientific computing problems arising in the physical/engineering sciences and other fields. Provides an introduction to efficient serial and parallel computing using Fortran 90, OpenMP, MPI, and Python, and software development tools such as version control, Makefiles, and debugging.", "smallIcon"=>"https://d15cw65ipctsrr.cloudfront.net/00/621b9b2597807229ed0fa605f96cdc/HighPerformanceComputingIma.jpg", "links"=>{}}, {"id"=>87, "shortName"=>"compphoto", "name"=>"Computational Photography", "shortDescription"=>"In this course you will learn about the basics of
....
或者你可以这样做:
>>> df.my_fruits = df.apply(lambda x: 'not_fruit' if x['fruit_a'] == 'vegetable' or x['fruit_b'] == 'vegetable' else x['my_fruits'], axis=1)
0 not_fruit
1 fruit
2 not_fruit
3 not_fruit
4 fruit