简体   繁体   English

使用fillna(method ='bfill')和group by的Python3熊猫数据框

[英]Python3 pandas data frame using fillna(method='bfill') with group by

I am new to python and pandas and stuck with request mentioned below Have data in python pandas data frame as 我是python和pandas的新手,并且卡在下面提到的请求中,因此python pandas数据框中的数据为

time_stamp        dish_id  table_no order_id
2017-10-05 22:11   122       A1 
2017-10-05 22:14   127       A1 
2017-10-05 22:17   129       A5 
2017-10-05 22:19   122       A1      X_001
2017-10-05 22:17   129       A5      X_002

I am filling in the missing order values with 我要填写缺少的订单值

output_sort[['new_order_id']] = output_sort[['order_id']].fillna(method='bfill')

and this gets me result like 这让我的结果像

time_stamp        dish_id  table_no order_id
2017-10-05 22:11   122       A1      X_001
2017-10-05 22:14   127       A1      X_001
2017-10-05 22:17   129       A5      X_001
2017-10-05 22:19   122       A1      X_001
2017-10-05 22:17   129       A5      X_002

However i want to get results like 但是我想得到像

time_stamp        dish_id  table_no order_id
2017-10-05 22:11   122       A1      X_001
2017-10-05 22:14   127       A1      X_001
2017-10-05 22:17   129       A5      X_002
2017-10-05 22:19   122       A1      X_001
2017-10-05 22:17   129       A5      X_002

The order_id's get matched with correct_table no I haven't been able to find a way to do it Any help would be really appreciated order_id已与correct_table匹配,否,我一直无法找到一种方法来做,任何帮助将不胜感激

df.groupby('table_no')['order_id'].apply(lambda x :x.ffill().bfill())
Out[529]: 
0    X_001
1    X_001
2    X_002
3    X_001
4    X_002
Name: order_id, dtype: object

df['order_id']=df.groupby('table_no')['order_id'].apply(lambda x :x.ffill().bfill())
df
Out[530]: 
        time_stamp  dish_id table_no order_id
0  2017-10-0522:11      122       A1    X_001
1  2017-10-0522:14      127       A1    X_001
2  2017-10-0522:17      129       A5    X_002
3  2017-10-0522:19      122       A1    X_001
4  2017-10-0522:17      129       A5    X_002
df.assign(order_id=df.groupby('table_no').order_id.bfill())

         time_stamp  dish_id table_no order_id
0  2017-10-05 22:11      122       A1    X_001
1  2017-10-05 22:14      127       A1    X_001
2  2017-10-05 22:17      129       A5    X_002
3  2017-10-05 22:19      122       A1    X_001
4  2017-10-05 22:17      129       A5    X_002

While not as idiomatic as bfill , map should be a pretty good alternative. 虽然不如bfill惯用, bfill map应该是一个不错的选择。

m = dict(df[['table_no', 'order_id']].dropna().values)

print(m)
{'A1': 'X_001', 'A5': 'X_002'}

df['order_id'] = df.table_no.map(m)

print(df)
         time_stamp  dish_id table_no order_id
0  2017-10-05 22:11      122       A1    X_001
1  2017-10-05 22:14      127       A1    X_001
2  2017-10-05 22:17      129       A5    X_002
3  2017-10-05 22:19      122       A1    X_001
4  2017-10-05 22:17      129       A5    X_002

You can also do this with df.replace : 您也可以使用df.replace进行此df.replace

df['order_id'] = df.table_no.replace(m)

print(df)
         time_stamp  dish_id table_no order_id
0  2017-10-05 22:11      122       A1    X_001
1  2017-10-05 22:14      127       A1    X_001
2  2017-10-05 22:17      129       A5    X_002
3  2017-10-05 22:19      122       A1    X_001
4  2017-10-05 22:17      129       A5    X_002

Another way of generating m would be: 生成m另一种方法是:

m = df[['table_no', 'order_id']].dropna().set_index('table_no').order_id
print(m)
table_no
A1    X_001
A5    X_002
Name: order_id, dtype: object
series_ = df.table_no.tolist()

def fill_():
    order_id_ = []
    if table_no == 'A1'
        order_id_.append('X_001')
    else:
        order_id_.append('X_005')
    return order_id_

df.order_no = list(map(fill_,series_))

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

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