Pandas 用逗号将列分成多列 [英] Pandas split column into multiple columns by comma
问题描述
我正在尝试根据逗号/空格分隔将一列拆分为多列.
I am trying to split a column into multiple columns based on comma/space separation.
我的数据框目前看起来像
My dataframe currently looks like
KEYS 1
0 FIT-4270 4000.0439
1 FIT-4269 4000.0420, 4000.0471
2 FIT-4268 4000.0419
3 FIT-4266 4000.0499
4 FIT-4265 4000.0490, 4000.0499, 4000.0500, 4000.0504,
我愿意
KEYS 1 2 3 4
0 FIT-4270 4000.0439
1 FIT-4269 4000.0420 4000.0471
2 FIT-4268 4000.0419
3 FIT-4266 4000.0499
4 FIT-4265 4000.0490 4000.0499 4000.0500 4000.0504
我的代码当前删除了 KEYS 列,我不确定为什么.有人可以改进或帮助解决问题吗?
My code currently removes The KEYS column and I'm not sure why. Could anyone improve or help fix the issue?
v = dfcleancsv[1]
#splits the columns by spaces into new columns but removes KEYS?
dfcleancsv = dfcleancsv[1].str.split(' ').apply(Series, 1)
推荐答案
如果其他人想要将单列(由值分隔)拆分为多列 - 试试这个:
In case someone else wants to split a single column (deliminated by a value) into multiple columns - try this:
series.str.split(',', expand=True)
这回答了我来这里寻找的问题.
This answered the question I came here looking for.
归功于 EdChum 的代码,其中包括将拆分列添加回数据帧.
Credit to EdChum's code that includes adding the split columns back to the dataframe.
pd.concat([df[[0]], df[1].str.split(', ', expand=True)], axis=1)
注意:第一个参数df[[0]]
是DataFrame
.
第二个参数 df[1].str.split
是您要拆分的系列.
The second argument df[1].str.split
is the series that you want to split.
这篇关于Pandas 用逗号将列分成多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!