简体   繁体   English

有条件地在Pandas数据框中填充值

[英]Conditionally filling in values in a Pandas dataframe

I am trying to reshape the Pandas dataframe on the left into the one on the right. 我正在尝试将左侧的Pandas数据框重塑为右侧的数据框。

It's easy enough to copy the type over 复制类型很容易
df['type'] = np.where(df.customer.str.match('DRIVER'), 'DRIVER', '')
but I need some way to capture the whole interval between IDs. 但是我需要一些方法来捕获ID之间的整个间隔。 This is the tough part. 这是困难的部分。 Any help or advice in general would be greatly appreciated. 一般而言,任何帮助或建议都将不胜感激。

在此处输入图片说明

df = { 'customer': ['ID 100', 'DRIVER', 'big wheel', 'unicycle', 'porshe', 'ID 200', 'EATER', 'bigmac', 'celery', 'gum', 'ID 300', 'MISSING', 'ID 400', 'READER', 'Gorden Korman', 'Hiroyuki Nishigaki'],
         'type': ['', '', '' , '', '', '', '', '', '', '', '', '', '', '', '', '']}
df = pd.DataFrame(df)

My solution is based on ID 100, ID 200, DRIVER, EATER, MISSING etc. being upper case. 我的解决方案基于大写的ID 100,ID 200,DRIVER,EATER,MISSING等。

Then use a map function followed by a fillna(method = 'ffill') and finally set back the "ID XXX"s to empty string. 然后使用map函数,后跟fillna(method ='ffill'),最后将“ ID XXX”设置回空字符串。

df['type'] =  df['customer'].map(lambda x: x if x.isupper() else None)
df['type'] = df['type'].fillna(method ="ffill")
df['type'] = df['type'].map(lambda x: '' if x[0:3] =='ID ' else x)

print df.head(len(df))



              customer     type
0               ID 100         
1               DRIVER   DRIVER
2            big wheel   DRIVER
3             unicycle   DRIVER
4               porshe   DRIVER
5               ID 200         
6                EATER    EATER
7               bigmac    EATER
8               celery    EATER
9                  gum    EATER
10              ID 300         
11             MISSING  MISSING
12              ID 400         
13              READER   READER
14       Gorden Korman   READER
15  Hiroyuki Nishigaki   READER

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

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