在列中拆分值并创建列名矩阵 [英] Split values in a column and create a matrix of column names

查看:44
本文介绍了在列中拆分值并创建列名矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以最小的努力来解决自己的问题.

I would like to have a solution for my problem with minimum effort.

问题:

我有一个带有定界值的值列表.我想在适当的单元格拆分并排列每个值.列标题也应填充.

I have a list of values with delimited values. I would like to split and arrange each values at the appropriate cell. Column Heading should be also populated.

A,B,C
C,D,A,E
D,E

输出

+-------+-------+-------+-------+-------+
| VLUE1 | VLUE2 | VLUE3 | VLUE4 | VLUE5 |
+-------+-------+-------+-------+-------+
| A     | B     | C     |       |       |
| A     |       | C     | D     | E     |
|       |       |       | D     | E     |
+-------+-------+-------+-------+-------+

我有一个在Python中使用排序,键值对并进行迭代的解决方案,但我想知道使用Python包或熊猫有没有捷径?

I have a solution using sorting, key value pair in python and iterating but i would like to know is there any shortcut using Python packages or panda?

-山姆

推荐答案

从系列开始-

s

0      A,B,C
1    C,D,A,E
2        D,E
dtype: object

使用get_dummies-

x = s.str.get_dummies(sep=',')
x

   A  B  C  D  E
0  1  1  1  0  0
1  1  0  1  1  1
2  0  0  0  1  1

使用它使用repeat和数组乘法创建新的数据框-

Use this to create a new dataframe using repeat and array multiplication -

v = x.mul(x.columns).values
c = np.arange(1, x.shape[1] + 1)

df = pd.DataFrame(v, columns=c).add_prefix('VLUE') 
df

  VLUE1 VLUE2 VLUE3 VLUE4 VLUE5
0     A     B     C            
1     A           C     D     E
2                       D     E

这篇关于在列中拆分值并创建列名矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆