简体   繁体   English

Pandas:如果来自第三列的字符串值,则根据另一列的值创建列

[英]Pandas : Create columns based on values of another column if string value from 3rd column

My dataframe is this :我的数据框是这样的:

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     
   

The wanted output is this:想要的输出是这样的:

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]              

So, based on the labels column, create 3 new columns with suffix the label value and endfix .position and use as values the corresponding position based on the label.因此,基于labels列,创建 3 个新列,其后缀为标签值和尾缀.position ,并将基于标签的相应位置用作值。

Is there a smart way to do it?有没有聪明的方法来做到这一点?

Use DataFrame.dropna for remove original column with missing values, then convert index to column, so possible use DataFrame.pivot , last add DataFrame.add_suffix :使用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.

相关问题 比较来自相同 pandas dataframe 的 2 列的值和基于比较的第 3 列的返回值 - comparing values of 2 columns from same pandas dataframe & returning value of 3rd column based on comparison 根据 pandas 上另一列值的值创建一列 - Create a column based on a value from another columns values on pandas 熊猫按两列分组,并从第三列输出值 - Pandas groupby two columns and output values from 3rd column 是否有一种SQL语法会根据同一表中第3列的相等值搜索2列来创建新列? - Is there a SQL syntax that will create new column by searching in 2 columns based on equal value of 3rd column in same table? Python 比较 2 列并用第 3 列中的值写入第 4 列(熊猫) - Python Compare 2 Columns And Write A 4th Column With Values From 3rd Column (pandas ) 通过过滤 Pandas 中的其他 2 列,在第 3 列中获取唯一值 - get unique values in a 3rd column by filtering 2 other columns in Pandas Python Pandas - 检查两列中的值,对第三列求和 - Python Pandas - check value in two columns, sum the 3rd column 取具有相同值 x 或不同值 x/y 的 2 列来创建具有值 x 的第 3 列 - Taking 2 columns with equal value x or differing values x/y to create a 3rd column with value x 在 groupby 2 列之后从第 3 列获取相应的值 - obtain corresponding values from 3rd column after groupby 2 columns Pandas 使用其他列的值创建新列,根据列值选择 - Pandas create new column with values from other columns, selected based on column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM