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

时间:2015-02-26 19:10:34

标签: python dataframe

如何将列值拆分为多行?

例如:

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

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

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

1 个答案:

答案 0 :(得分:0)

使用'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