繁体   English   中英

Python:将数据框行拆分为多行

[英]Python: split data frame row into multiple rows

如何将列值拆分为多行?

例如 :

我有以下标题: title, url, body, comments 我删除了这些数据并将其导出为csv文件。 我的评论专栏有多条评论,如下所示:

{'comment': [u'Have you never see Eric Pickles?', u'Looks a bit unrealistic'], 'name': [u'gruniadreader666', u'Dowling1981']}

我希望每条评论都在自己的行中。

使用'DataFrame.from_dict'

In [1]: import pandas

In [2]: d = {'comment': [u'Have you never see Eric Pickles?', u'Looks a bit unrealistic'], 'name': [u'gruniadreader666', u'Dowling1981']}


In [3]: pandas.DataFrame.from_dict(d, orient='columns')
Out[3]: 
                            comment              name
0  Have you never see Eric Pickles?  gruniadreader666
1           Looks a bit unrealistic       Dowling1981

您也可以围绕索引进行定位,但在这种情况下,这不是您想要的。

In [4]: pandas.DataFrame.from_dict(d, orient='index')
Out[4]: 
                                        0                        1
comment  Have you never see Eric Pickles?  Looks a bit unrealistic
name                     gruniadreader666              Dowling1981

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM