简体   繁体   English

根据来自另一列的信息用熊猫填充一个空列

[英]Fill an empty column according to information from another column with pandas

I'm trying to fill an empty column according to information from another column 我正在尝试根据另一列中的信息填充空白列

My dataframe 我的数据框

   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

So, I create a D column and if column C starts with 'description', I fill in 'fuzzy', if not with 'buzzy'. 因此,我创建了一个D列,如果列C以'description'开头,则填写'fuzzy',如果不是以'buzzy'填充。

new_column['D'] = ''

And I try to fill them 我尝试填补他们

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

My output: 我的输出:

  File "<ipython-input-41-ec3c1407168c>", line 6
    else:
       ^
SyntaxError: invalid syntax

Good output: 好的输出:

   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

You don't need if-else statements here, you can do this cleanly in a single line using np.where : 您在这里不需要if-else语句,可以使用np.where在一行中干净地完成此np.where

df['D'] = np.where(
    df['C'].str.startswith('description:'), 'fuzzy', 'buzzy')

You can do this with a single loc call, since you're assigning two values only. 您可以通过一个loc调用来完成此操作,因为您仅分配了两个值。

df['D'] = 'buzzy'
df.loc[df['C'].str.startswith('description:'), 'D'] = 'fuzzy'

Or use df.mask / df.where like @jpp suggested in the comments: 或使用df.mask / df.where如注释中建议的@jpp:

df['D'] = 'buzzy'
df['D'] = df['D'].mask(df['C'].str.startswith('description:'), 'fuzzy')

And lastly, using map : 最后,使用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
new_column.loc[new_column['C'].str.startswith('description:'), 'D'] = 'fuzzy'
new_column.loc[~new_column['C'].str.startswith('description:'), 'D'] = 'buzzy'

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

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