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. All CSV files have same type of data format. A sample of the data present:
| 0 | |
|---|---|
| 1 | Random Place 1 · |
| 4.5 · | |
| 121 Random Street · | |
| 2 | Random Place 2 · |
| 4.5 · | |
| 121 Random Street · |
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. Can anyone help me write a loop to iterate the process?
CodePudding user response:
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.
