简体   繁体   English

如何在 Pandas 中将特定列拆分为新列?

[英]How can I split a specific column to new columns in Pandas?

I want to split "rest" column to new columns by comma and drop "R=".我想用逗号将“rest”列拆分为新列并删除“R=”。 And also add +1 to "joints" column.并将 +1 添加到“关节”列。 How can i do?我能怎么做?

df
     joints           rest
          0  R=0,0,1,1,1,1
          3  R=0,0,1,1,1,1
         42  R=0,0,1,1,1,1
         45  R=0,0,1,1,1,1

I want to do like this:我想这样做:

joints U1 U2 U3 R1 R2 R3
1      0  0  1  1  1  1
4      0  0  1  1  1  1
43     0  0  1  1  1  1
46     0  0  1  1  1  1

For more dynamic rename columns names is used function with lambda, for new columns is used Series.str.split with expand=True and assign back to original by DataFrame.join :对于更动态的重命名列名称,使用带有 lambda 的函数,对于新列,使用带有expand=True Series.str.split并通过DataFrame.join分配回原始DataFrame.join

f = lambda x: f'U{x+1}' if x < 3 else f'R{x-2}' 
df1 = (df.join(df.pop('rest').str.split('=')
                             .str[1]
                             .str.split(',', expand=True)
                             .rename(columns=f))
          .assign(joints = df['joints'] + 1))
print (df1)
   joints U1 U2 U3 R1 R2 R3
0       1  0  0  1  1  1  1
1       4  0  0  1  1  1  1
2      43  0  0  1  1  1  1
3      46  0  0  1  1  1  1

Here's one approach.这是一种方法。 Since there's no specified criteria for the column namings, I've just hardcoded in this case:由于没有为列命名指定标准,因此在这种情况下我只是硬编码:

cols = ['U1', 'U2', 'U3', 'R1', 'R2', 'R3']
out = (df.rest.str.lstrip('R=')
              .str.split(',', expand=True)
              .rename(columns=dict(zip(range(len(cols)), cols)))
out['joints'] = df.joints.add(1)

  U1 U2 U3 R1 R2 R3  joints
0  0  0  1  1  1  1       1
1  0  0  1  1  1  1       4
2  0  0  1  1  1  1      43
3  0  0  1  1  1  1      46

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将包含 json 的 pandas 列拆分为给定 dataframe 中的新列? - How can I split a pandas column containing a json into new columns in a given dataframe? Pandas dataframe,如何按多列分组并为特定列应用总和并添加新的计数列? - Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column? 列存储为列表; 如何在 pandas python 中拆分为 COLUMNS? - Column stored as List; how can I split as COLUMNS in pandas python? 如何将 Pandas 数组拆分为列? - How can I split Pandas arrays into columns? 使用 Pandas Dataframe,如何拆分特定列中的字符串,然后用拆分的第一个索引替换该字符串? - Using a Pandas Dataframe, how can I split the strings in a specific column and then replace that string with the first index of the split? 如何在具有特定分隔符位置的熊猫中将列拆分为多列? - How to split column into multiple columns in pandas with specific delimiter position? Pandas - 如何通过特定字符的索引将字符串列拆分为几列? - Pandas - How to split a string column into several columns, by the index of specific characters? 如何将数据从 Pandas 数据帧的一列拆分为新数据帧的多列 - How do I split data out from one column of a pandas dataframe into multiple columns of a new dataframe 如何根据价值在熊猫中拆分一列并创建新列? - how to split one column in pandas based on value and create new columns? 如何将单个 Pandas Dataframe 列的内容拆分为多个新列 - How to Split the Contents of a Single Pandas Dataframe Column into Multiple New Columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM