如何拆分python中的DataFrame列 [英] How to split a DataFrame column in python

查看:1552
本文介绍了如何拆分python中的DataFrame列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我要分割的DataFrame列。
例如:

  0 1 2 
0 ab cde
1 fg hij
2 kl mno

信息存储在DataFrame中。如何将数据框更改为此?

  0 1 2 3 4 
0 abcde
1 fghij
2 klmno

谢谢

解决方案

您正在寻找 Series.str.split

 >> ;> print df [2] .str.split(' - ',expand = True)
0 1 2
0 cde
1 hij
2 mno

>>> pd.concat((df [[0,1]],df [2] .str.split(' - ',expand = True)),axis = 1,ignore_index = True)
0 1 2 3 4
0 abcde
1 fghij
2 klmno

使用大于0.16.1的大熊猫,您要使用 return_type ='frame'而不是 expand = True / p>

i have DataFrame column which i want to split. For example:

     0     1     2
0    a     b     c-d-e
1    f     g     h-i-j
2    k     l     m-n-o

The information is stored in a DataFrame. How can I change the dataframe to this?

     0     1     2     3     4
0    a     b     c     d     e
1    f     g     h     i     j
2    k     l     m     n     o

Thank you

解决方案

You're looking for Series.str.split

>>> print df[2].str.split('-', expand=True)
   0  1  2
0  c  d  e
1  h  i  j
2  m  n  o

>>> pd.concat((df[[0, 1]], df[2].str.split('-', expand=True)), axis=1, ignore_index=True)
   0  1  2  3  4
0  a  b  c  d  e
1  f  g  h  i  j
2  k  l  m  n  o

If you're using pandas older than 0.16.1, you want to use return_type='frame' instead of expand=True

这篇关于如何拆分python中的DataFrame列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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