【发布时间】:2019-07-31 10:26:01
【问题描述】:
假设我们有一个如下的df
df = pd.DataFrame({ 'Col1' : [1, 1, 1, 2, 2, 2, 2],
'Col2' : [5, 6, 8, 3, 7, 8, 5],
'Col3' : [2, None, None, 3, None, None, 4],
'Col4' : [3, None,5, None, 8, None, 66],
'Col5': [None, 8, 6, None, 9, 6,None],
'Col6' : [3,5,2,5,2,7,9]})
在第一列Col1 应用groupby 后,我想使用jjs in this post here 建议的解决方案替换Col3、Col4 和Col5 列中的None 值。
我的做法是
df = df.groupby('Col1')['Col3','Col4','Col5'].ffill().bfill()
但手动提及列需要做很多工作。
所以,我想知道如何通过切片选择列Col3、Col4 和Col5?
谢谢
【问题讨论】:
-
切片选择是什么意思?
-
类似于使用
.iloc[2:5]来获取所需的列
标签: python python-3.x pandas dataframe group-by