[英]How to read csv file in pandas as two column from multiple delimiter values
I've a csv file like this:我有一个像这样的 csv 文件:
123, a, b, c, d
1433, b, c, d, e
2323, c, d, e, f
4543, d, e, f
I want to read this into dataframe but I want the first delimiter value as one column and rest as another column我想将其读入数据帧,但我希望第一个分隔符值作为一列,其余作为另一列
id values
123 a, b, c, d
1433 b, c, d, e
2323 c, d, e, f
4543 d, e, f, NaN
I tried to use pandas read_csv but i couldn't find a option such as maxsplit there.
我尝试使用pandas read_csv,但在那里找不到诸如maxsplit之类的选项。 If anyone is familiar with how to do it do help me out.
如果有人熟悉如何做到这一点,请帮助我。
I put in a wrong delimiter in the read_csv function, which forces Pandas to read the data into one column, from there I split the column into the format I want.我在 read_csv 函数中输入了错误的分隔符,这会强制 Pandas 将数据读入一列,然后我将列拆分为我想要的格式。 Note however, this does not trump Datanovice's solution, as the NaN is not introduced:
但是请注意,这并不能胜过 Datanovice 的解决方案,因为没有引入 NaN:
data = '''123, a, b, c, d
1433, b, c, d, e
2323, c, d, e, f
4543, d, e, f'''
df = pd.read_csv(StringIO(data),sep=';', header= None, names=['string'])
df.string.str.split(pat=',', n=1,expand=True)
0 1
0 123 a, b, c, d
1 1433 b, c, d, e
2 2323 c, d, e, f
3 4543 d, e, f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.