简体   繁体   English

将列值拆分为多列熊猫/ python

[英]Split a column value to mutliple columns pandas /python

I am new to Python/Pandas and have a data frame with two columns one a series and another a string. 我是Python / Pandas的新手,并且有一个包含两列的数据框,一个列是一个序列,另一个列是一个字符串。 I am looking to split the contents of a Column(Series) to multiple columns .Appreciate your inputs on this regard . 我希望将Column(Series)的内容拆分为多个列。对此表示赞赏。 This is my current dataframe content 这是我当前的数据框内容

    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

Desired output is SONG , ARTIST , SONG ID ,DENSITY ie split song details into columns. 所需的输出是SONG,ARTIST,SONG ID,DENSITY,即,将歌曲详细信息分为几列。

for eg for the sample data 用于例如样本数据

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

SONG   ARTIST              SONG ID               DENSITY

2tall  Demonstration    SOYYQZR12A8C144F9D         5.472524

Thanks 谢谢

The following worked for me: 以下为我工作:

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

For you please try: pd.DataFrame(data = list(df['Songdetails'].values), columns = ['SONG', 'ARTIST', 'SONG ID']) 为您服务,请尝试: pd.DataFrame(data = list(df['Songdetails'].values), columns = ['SONG', 'ARTIST', 'SONG ID'])

谢谢,我在新数据框架中插入了列,并能够实现我需要的功能,谢谢df2 = pd.DataFrame(series.apply(lambda x:pd.Series(x.split(',') )))df2.insert(3,'Density',finaldf ['Density'])

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

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