代码通过遍历每一行并调用'is_color'函数来工作。该函数检查第i行中的值并指定颜色,例如,如果满足条件,则为“蓝色”
import numpy as np
import pandas as pd
def is_color(df):
df['color'] = np.nan
def blue(i):
is_blue = True # some more complex condition
if is_blue:
#df['color'].iloc[i] = 'blue'
df.set_value(i, 'color', 'blue')
for i in range(len(df)):
blue(i)
# not included i this example
#green(i)
#orange(i)
#purple(i)
#yellow(i)
return df
我最初做的df['color'].iloc[i] = 'blue'
虽然有效但投了SettingWithCopyWarning
我需要让它准备就绪,我尝试了df.set_value(i, 'color', 'blue')
但是我想要做ValueError: could not convert string to float: blue
它是这样的我认为:
import numpy as np
import pandas as pd
def is_color(df):
df['color'] = np.nan
def blue(i, df):
is_blue = True # some more complex condition
if is_blue:
#df['color'].iloc[i] = 'blue'
return df.set_value(i, 'color', 'blue')
return df
for i in range(len(df)):
df = blue(i, df)
# not included i this example
#df = green(i, df)
#df = orange(i, df)
return df
我觉得我的原始代码更干净了,有没有更漂亮的方法呢?
答案 0 :(得分:0)
如果可能有多种情况,请apply
使用if
,elif
和else
自定义功能:
样品:
df = pd.DataFrame({'A':[10,20,31],
'B':[4,5,6]})
print (df)
def is_color(x):
if x < 15:
x = 'blue'
elif (x > 15) and (x < 25):
x = 'green'
else:
x = 'nothing'
return (x)
df['color'] = df['A'].apply(is_color)
print (df)
A B color
0 10 4 blue
1 20 5 green
2 31 6 nothing
类似的解决方案:
def is_color(x):
a = 'nothing'
if x < 15:
a = 'blue'
if (x > 15) and (x < 25):
a = 'green'
return (a)
df['color'] = df['A'].apply(is_color)
print (df)
A B color
0 10 4 blue
1 20 5 green
2 31 6 nothing