简体   繁体   English

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

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

How can I split column values into many rows ? 如何将列值拆分为多行?

For example : 例如 :

I have the following headers: title, url, body, comments . 我有以下标题: title, url, body, comments I scraped this data and exported it as a csv file. 我删除了这些数据并将其导出为csv文件。 My comment column has more than one comment as shown below : 我的评论专栏有多条评论,如下所示:

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

I want each comment to be in its own row. 我希望每条评论都在自己的行中。

Use 'DataFrame.from_dict' 使用'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

You can also orient around the index, but that's not what you want in this case. 您也可以围绕索引进行定位,但在这种情况下,这不是您想要的。

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