过滤数据框并根据给定条件添加新列

时间:2019-05-13 17:55:11

标签: python python-3.x pandas numpy pandas-groupby

我有一个这样的数据框

ID      col1           col2 
1    Abc street       2017-07-27 
1    None             2017-08-17 
1    Def street       2018-07-15 
1    None             2018-08-13 
2    fbg street       2018-01-07 
2    None             2018-08-12 
2    trf street       2019-01-15 

我想过滤col1中的所有“无”并将相应的col2值添加到新列col3中。我的输出看起来像这样

ID      col1           col2              col3 
1    Abc street       2017-07-27     2017-08-17          
1    Def street       2018-07-15     2018-08-13             
2    fbg street       2018-01-07     2018-08-12             
2    trf street       2019-01-15     

任何人都可以帮助我实现这一目标。

5 个答案:

答案 0 :(得分:10)

今天的《 Over Engineering with Numpy》版

尽管公认的Numpy很少见

i, rows = pd.factorize([*zip(df.ID, df.col1.replace('None'))])
k, cols = pd.factorize(df.groupby(i).cumcount())

dleft = pd.DataFrame(dict(zip(['ID', 'col1'], zip(*rows))))
drigt = pd.DataFrame(index=dleft.index, columns=np.arange(len(cols)) + 2).add_prefix('col')
drigt.values[i, k] = df.col2.values

dleft.join(drigt)

   ID        col1        col2        col3
0   1  Abc street  2017-07-27  2017-08-17
1   1  Def street  2018-07-15  2018-08-13
2   2  fbg street  2018-01-07  2018-08-12
3   2  trf street  2019-01-15         NaN

答案 1 :(得分:8)

我将cumcountmerge一起使用

df1=df.loc[df.col1.ne('None'),:].copy()
df2=df.loc[df.col1.eq('None'),:].copy()
df1['Key']=df1.groupby('ID').cumcount()
df2['Key']=df2.groupby('ID').cumcount()
df1.merge(df2.drop('col1',1),on=['ID','Key'],how='left')
Out[816]: 
   ID       col1      col2_x  Key      col2_y
0   1  Abcstreet  2017-07-27    0  2017-08-17
1   1  Defstreet  2018-07-15    1  2018-08-13
2   2  fbgstreet  2018-01-07    0  2018-08-12
3   2  trfstreet  2019-01-15    1         NaN

答案 2 :(得分:7)

使用ffill + pivot_table。假设None遵循正确的值,该值会从您的数据中显示出来。


u = df.assign(col1=df.col1.replace('None'))
g = ['ID', 'col1']
idx = u.groupby(g).cumcount()

(u.assign(idx=idx)
    .pivot_table(index=g, columns='idx', values='col2', aggfunc='first')
    .reset_index())  

idx   ID        col1           0           1
0      1  Abc street  2017-07-27  2017-08-17
1      1  Def street  2018-07-15  2018-08-13
2      2  fbg street  2018-01-07  2018-08-12
3      2  trf street  2019-01-15         NaN

答案 3 :(得分:4)

尝试:

filters = df['col1'].isna()
s = df.loc[filters, 'col2'].copy()
df = df[~filters]
df['col3'] = s.values

编辑:如前所述,您想要的过滤器是'None',而不是None,然后:

filters = df['col1'].eq('None')

答案 4 :(得分:1)

再尝试一次

f=df['col1']=='None'
c3=df.loc[f].col2.reset_index(drop=True)
df=df[~f]
df2=pd.concat([df.reset_index(drop=True),c3], axis=1, ignore_index=True)
df2.columns=['ID', 'col1', 'col2', 'col3']

   ID        col1        col2        col3
0   1  Abc street  2017-07-27  2017-08-17
1   1  Def street  2018-07-15  2018-08-13
2   2  fbg street  2018-01-07  2018-08-12
3   2  trf street  2019-01-15         NaN