我有一个看起来像这样的数据结构:
idtenifier amount dist_type new_value new_value2
1 1.0 normal
1 2.0 new_value
1 1.0 new_value2
3 1.0 normal
5 3.0 normal
5 23.0 new_value2
2 1.0 normal我希望得到这样的结构:
idtenifier amount dist_type new_value new_value2
1 1.0 normal 2.0 1.0
3 1.0 normal 23.0
5 3.0 normal
2 1.0 normal我有一种感觉,我试图这样做是非常低效的,我甚至不能在列中赋值
df['new_value'] = np.nan
for idx, row in df.iterrows():
identifier = row['identifier']
dist_type = row['dist_type']
amount = row['amount']
if idx > 0 and identifier == df.loc[idx-1, 'identifier']:
print(dist_type)
if dist_type == 'new_value':
df.loc[idx-1, 'new_value'] == amount发布于 2019-03-15 08:42:00
在这里我们不需要使用for循环,在将数据帧一分为二之后,对于dist_type不等于normal,我们执行pivot,然后merge它回来
df1=df.loc[df.dist_type=='normal'].copy()
df2=df.loc[df.dist_type!='normal'].copy()
yourdf=df1.merge(df2.pivot('idtenifier','dist_type','amount').reset_index(),how='left')
yourdf
Out[33]:
idtenifier amount dist_type new_value new_value2
0 1 1.0 normal 2.0 1.0
1 3 1.0 normal NaN NaN
2 5 3.0 normal NaN 23.0
3 2 1.0 normal NaN NaNhttps://stackoverflow.com/questions/55173951
复制相似问题