[英]Add new columns to DataFrame from the value of one column
I have a pandas DataFrame, there is a column with values like a,b,c ie string splited by ','. 我有一个熊猫DataFrame,有一列的值,如a,b,c,即用','分割的字符串。 Now I want to create new columns, for example, for a,b,c there would be new column a, column b, column c.
现在,我想创建新列,例如,对于a,b,c,将有新列a,b列,c列。 then the data with a,b,c would get a value of true on the three columns, the data with a,b,e would get true on columns a and b but false on c, maybe it is more clearly to see the picture below.
那么a,b,c的数据在三列中的值为true,a,b,e的数据在a和b列中的值为true,而c的值为false,也许更清楚地看到图片下面。 How to do this?
这个怎么做?
Use str.get_dummies
with cast to bool by astype
and add column B
by join
: 使用
str.get_dummies
和str.get_dummies
按类型astype
为bool并通过join
添加B
列:
df1 = df['A'].str.get_dummies(',').astype(bool).join(df['B'])
print (df1)
a b c f B
0 True True True False 3
1 False True True True 4
More general solution with pop
for extract column A
: 提取列
A
pop
更通用的解决方案:
df = pd.DataFrame({'A':['a,b,c','b,c,f'], 'B':[3,4], 'C':[7,3]})
print (df)
A B C
0 a,b,c 3 7
1 b,c,f 4 3
df1 = df.pop('A').str.get_dummies(',').astype(bool).join(df)
print (df1)
a b c f B C
0 True True True False 3 7
1 False True True True 4 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.