简体   繁体   中英

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 . I scraped this data and exported it as a csv file. 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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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