我正在尝试根据另一列中的信息填充空白列
我的数据框
A B C
0 F House Are you at home?
1 E House description: to deliver tomorrow
2 F Apt Here is some exemples
3 F House description: a brown table
4 E Apt description: in the bus
5 F House Hello, how are you?
6 E Apt description: keys
因此,我创建了一个D列,如果列C以'description'开头,则填写'fuzzy',如果不是'buzzy',则填写
。new_column['D'] = ''
然后我尝试填充它们
def fill_column(delete_column):
if new_column['D'].loc[new_column['D'].str.startswith('description:'):
new_column['D'] == 'fuzzy'
else:
new_column['D'] == 'buzzy'
return new_column
我的输出:
File "<ipython-input-41-ec3c1407168c>", line 6
else:
^
SyntaxError: invalid syntax
好的输出:
A B C D
0 F House Are you at home? buzzy
1 E House description: to deliver tomorrow fuzzy
2 F Apt Here is some exemples buzzy
3 F House description: a brown table fuzzy
4 E Apt description: in the bus fuzzy
5 F House Hello, how are you? buzzy
6 E Apt description: keys fuzzy
答案 0 :(得分:5)
您在这里不需要if-else
语句,可以使用np.where
在一行中简洁地完成此操作:
df['D'] = np.where(
df['C'].str.startswith('description:'), 'fuzzy', 'buzzy')
由于您仅分配了两个值,因此可以通过一个loc
调用来完成此操作。
df['D'] = 'buzzy'
df.loc[df['C'].str.startswith('description:'), 'D'] = 'fuzzy'
或者使用df.mask
/ df.where
,例如注释中建议的@jpp:
df['D'] = 'buzzy'
df['D'] = df['D'].mask(df['C'].str.startswith('description:'), 'fuzzy')
最后,使用map
:
m = {True: 'fuzzy', False: 'buzzy'}
df['D'] = df['C'].str.startswith('description:').map(m)
print(df)
A B C D
0 F House Are you at home? buzzy
1 E House description: to deliver tomorrow fuzzy
2 F Apt Here is some exemples buzzy
3 F House description: a brown table fuzzy
4 E Apt description: in the bus fuzzy
5 F House Hello, how are you? buzzy
6 E Apt description: keys fuzzy
答案 1 :(得分:1)
new_column.loc[new_column['C'].str.startswith('description:'), 'D'] = 'fuzzy'
new_column.loc[~new_column['C'].str.startswith('description:'), 'D'] = 'buzzy'