将列值拆分为多个列pandas / python

时间:2014-10-10 14:54:03

标签: python pandas

我是Python / Pandas的新手,拥有一个数据框,其中包含两列,一列是一列,另一列是字符串。 我希望将列(系列)的内容拆分为多列。在这方面感谢您的输入。 这是我目前的数据框内容

    Songdetails                                         Density
0   ["'t Hof Van Commerce", "Chance", "SORETGR12AB...   4.445323
1   ["-123min.", "Try", "SOERGVA12A6D4FEC55"]           3.854437
2   ["10_000 Maniacs", "Please Forgive Us (LP Vers...   3.579846
3   ["1200 Micrograms", "ECSTACY", "SOKYOEA12AB018...   5.503980
4   ["13 Cats", "Please Give Me Something", "SOYLO...   2.964401
5   ["16 Bit Lolitas", "Tim Likes Breaks (intermez...   5.564306
6   ["23 Skidoo", "100 Dark", "SOTACCS12AB0185B85"]     5.572990
7   ["2econd Class Citizen", "For This We'll Find ...   3.756746
8   ["2tall", "Demonstration", "SOYYQZR12A8C144F9D"]    5.472524

所需的输出是SONG,ARTIST,SONG ID,DENSITY,即将歌曲细节分成列。

例如对于样本数据

           SONG DETAILS                                        DENSITY
8   ["2tall", "Demonstration", "SOYYQZR12A8C144F9D"]    5.472524

SONG   ARTIST              SONG ID               DENSITY

2tall  Demonstration    SOYYQZR12A8C144F9D         5.472524

谢谢

2 个答案:

答案 0 :(得分:0)

以下对我有用:

In [275]:

pd.DataFrame(data = list(df['Song details'].values), columns = ['Song', 'Artist', 'Song Id'])
Out[275]:
    Song         Artist             Song Id
0  2tall  Demonstration  SOYYQZR12A8C144F9D
1  2tall  Demonstration  SOYYQZR12A8C144F9D

请试试:pd.DataFrame(data = list(df['Songdetails'].values), columns = ['SONG', 'ARTIST', 'SONG ID'])

答案 1 :(得分:0)

谢谢,我有一个插入新数据框的列,并且能够实现我所需要的感谢df2 = pd.DataFrame(series.apply(lambda x:pd.Series(x.split(& #39;,')))) df2.insert(3,'密度',finaldf ['密度'])