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

时间:2017-10-05 18:46:45

标签: python pandas pandas-groupby

我是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

我用

填写缺少的订单值
output_sort[['new_order_id']] = output_sort[['order_id']].fillna(method='bfill')

这会让我得到像

的结果
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

但我希望得到像

这样的结果
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

order_id与correct_table no匹配 我一直无法找到办法 任何帮助都会非常感激

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

虽然不像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

您也可以使用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

生成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

答案 3 :(得分:0)

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_))