[英]Cannot Split CSV column to multiple columns in csv files
I have 5 CSV files : my+first.csv, my+second.csv..my+fifth.csv with one column labelled as 0 which has n rows of data but the second column has data with line breaks which I want to split into 3 columns : Name, User rating, Location. I have 5 CSV files : my+first.csv, my+second.csv..my+fifth.csv with one column labelled as 0 which has n rows of data but the second column has data with line breaks which I want to split分为3 列:名称、用户评分、位置。 All CSV files have same type of data format.所有 CSV 文件具有相同类型的数据格式。 A sample of the data present:提供的数据样本:
0 0 | |
---|---|
1 1 | Random Place 1 ·随机地点 1·· |
4.5 · 4.5·· | |
121 Random Street · 121 随机街 · | |
2 2 | Random Place 2 ·随机地点2·· |
4.5 · 4.5·· | |
121 Random Street · 121 随机街 · |
The code I wrote for this sample is:我为此示例编写的代码是:
import pandas as p
df = p.read_csv("sample+abc.csv")
b= df.columns.values.tolist()
c = df.columns.get_loc("0")
df['0']= df['0'].str.split("Â", n=1, expand = True)
print(b)
The result does not return separated columns.结果不返回分隔列。 Can any one help with the code?任何人都可以帮助代码吗? Also I want to do this for all csv files.我也想对所有 csv 文件执行此操作。 Can anyone help me write a loop to iterate the process?谁能帮我写一个循环来迭代这个过程?
Since noone answered my question, I'm posting how I managed to solve it:由于没有人回答我的问题,我发布了我如何设法解决它:
df1= df["your-column-name"].str.split("\n", expand=True)
I used \n
because I wanted to separate using line breaks.我使用\n
是因为我想使用换行符进行分隔。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.