[英]comparing values of 2 columns from same pandas dataframe & returning value of 3rd column based on comparison
[英]Pandas : Create columns based on values of another column if string value from 3rd column
我的数据框是这样的:
position labels
[58.0, 71.0] ind
[137.0, 147.0] pro
[170.0, 191.0] pro
[nan, nan] NaN
[nan, nan] NaN
[36.0, 57.0] pro
[67.0, 73.0] ind
[86.0, 93.0] tar
[0.0, 8.0] ind
想要的输出是这样的:
ind.position pro.position tar.position
[58.0, 71.0]
[137.0, 147.0]
[170.0, 191.0]
[36.0, 57.0]
[67.0, 73.0]
[86.0, 93.0]
[0.0, 8.0]
因此,基于labels
列,创建 3 个新列,其后缀为标签值和尾缀.position
,并将基于标签的相应位置用作值。
有没有聪明的方法来做到这一点?
使用DataFrame.dropna
删除缺少值的原始列,然后将索引转换为列,因此可以使用DataFrame.pivot
,最后添加DataFrame.add_suffix
:
df = (df.dropna(subset=['labels'])
.reset_index()
.pivot('index','labels','position')
.add_suffix('.position'))
print (df)
labels ind.position pro.position tar.position
index
0 [58.0,71.0] NaN NaN
1 NaN [137.0,147.0] NaN
2 NaN [170.0,191.0] NaN
5 NaN [36.0,57.0] NaN
6 [67.0,73.0] NaN NaN
7 NaN NaN [86.0,93.0]
8 [0.0,8.0] NaN NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.