【问题标题】:converting comma separated data of excel file into new line using python, pandas使用python,pandas将excel文件的逗号分隔数据转换为新行
【发布时间】:2018-11-29 20:49:52
【问题描述】:

我有一个excel文件,数据为

StudentId    Details
1234         John, Texas, United States
9887         Roma, Moscow, Russia

我想把它转换成以下格式,这样:

StudentId    Details
1234         John
             Texas
             United States 

9887         Roma
             Moscow
             Russia

我为此目的使用 Pandas,但没有得到结果

for i in range(len(df['Details'])):
    df['Details'][i]=df['Details'][i].replace(',','\n')

我在使用这种逻辑

【问题讨论】:

    标签: excel python-3.x pandas csv data-analysis


    【解决方案1】:

    read_fwf() 阅读它,然后再拆分该列:

    df['Details'].str.split(',')
    

    【讨论】:

    • import pandas as pd df = pd.read_fwf("data.xlsx") df['Details'].str.split(',') 我是这个领域的新手,我尝试使用按照你的建议,但它给出了一些不可读的输出
    • 哦,我错过了你正在阅读一个 excel 文件。见pd.read_excel()
    • 不,它在同一行给出结果:[' John', 'Texas', 'United States']
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多