简体   繁体   English

如何将 pipe 分隔字符串转换为 pandas dataframe 中列的列表 python

[英]how to convert pipe delimited string into list python for column in pandas dataframe

I need to convert csv file to json. In csv file email is delimited by pipe(|) in a string.我需要将 csv 文件转换为 json。在 csv 文件中,email 在字符串中由竖线 (|) 分隔。 Need to convert into list.需要转换成列表。 I am able to convert into list but only 1 email is coming in output.我能够转换成列表,但只有 1 email 会出现在 output 中。

Input Data in csf format以 csf 格式输入数据

first_name, last_name, email
"ABC","DEF","abc@gmail.com|def@gmail.com"
"CBA","FED","cba@gmail.com|fed@gmail.com"

Expected Ouput Data in json format json 格式的预期输出数据

[
    {
        "first_name" : "ABC",
        "last_name" : "DEF",
        "email" : ["abc@gmail","def@gmail"]
    }
    {
        "first_name" : "CBA",
        "last_name" : "DEF",
        "email" : ["abc@gmail","def@gmail"]
    }
]

Getting output获取 output

[
    {
        "first_name" : "ABC",
        "last_name" : "DEF",
        "email" : ["abc@gmail"]
    }
    {
        "first_name" : "CBA",
        "last_name" : "DEF",
        "email" : ["abc@gmail"]
    }
]

code tried代码试过

df = pd.read_csv("filename")
df.Email = df.Email.str.split('|')
df1 = df.to_json("filename", orient='records')

I am using pandas v1.0.5 and it works as expected.我正在使用 pandas v1.0.5,它按预期工作。

在此处输入图像描述

In your example of csv data the first line has spaces after the comma, but that would just throw an error, when trying to access df.email .在您的 csv 数据示例中,第一行在逗号后有空格,但这只会在尝试访问df.email时引发错误。

Conclusion: The error must be somewhere else.结论:错误一定是在别的地方。 Either you didn't share all relevant code with us, the data is actually different, or it actually does the job correctly and there is no error, but it's just how the data is.要么你没有与我们分享所有相关代码,数据实际上是不同的,要么它实际上正确地完成了工作并且没有错误,但它只是数据的样子。

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

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