Reputation: 65
I am loading a csv file in order to read it in a dataframe. But, when I load it, I got the dataframe which was not separated.
The data in a CSV file is as below,
Name;OffsetBarrierX;MeanF;MaxF;Thickness
sim1;88.416;-120140.0402;453487.6875;2.4
sim2;-108.321;-119949.118;447437.9688;2.4
sim3;-89.221;-119324.1906;576758.625;2.4
sim4;-89.221;-119324.1906;576758.625;2.4
sim5;-87.488;-117527.1688;574904.5625;2.4
sim6;-11.1424;-183188.6846;763354;2.4
I wrote followig line of code,
path = '../00_Data/Res_Overl_A.csv'
pd.read_csv(path, sep = ';')
I got the following output,
Name;OffsetBarrierX;MeanF;MaxF;Thickness
0 sim1;88.416;-120140.0402;453487.6875;2.4
1 sim2;-108.321;-119949.118;447437.9688;2.4
2 sim3;-89.221;-119324.1906;576758.625;2.4
3 sim4;-87.488;-117527.1688;574904.5625;2.4
4 sim5;-11.1424;-183188.6846;763354;2.4
5 sim6;-82.713;-121320.2933;608878.1875;2.4
6 sim7;-27.194;-172708.102;747944.625;2.4
So as you can see, there is no change. I need an output which has different data in different column of dataframe. How can I make different columns for every data?
Upvotes: 0
Views: 78
Reputation: 13582
Couldn't reproduce OP's problem as when I create a csv with the data indicated above, pandas.read_csv
reads without any issue
path = r'C:\Users\johndoe\Documents\Python\Challenges\StackOverflow\0108.csv'
df = pd.read_csv(path, sep=';')
[Out]:
Name OffsetBarrierX MeanF MaxF Thickness
0 sim1 88.4160 -120140.0402 453487.6875 2.4
1 sim2 -108.3210 -119949.1180 447437.9688 2.4
2 sim3 -89.2210 -119324.1906 576758.6250 2.4
3 sim4 -89.2210 -119324.1906 576758.6250 2.4
4 sim5 -87.4880 -117527.1688 574904.5625 2.4
5 sim6 -11.1424 -183188.6846 763354.0000 2.4
However, given OP's problem, one approach would be to store the content of the CSV in a variable. Let's call the variable csv
csv = """Name;OffsetBarrierX;MeanF;MaxF;Thickness
sim1;88.416;-120140.0402;453487.6875;2.4
sim2;-108.321;-119949.118;447437.9688;2.4
sim3;-89.221;-119324.1906;576758.625;2.4
sim4;-89.221;-119324.1906;576758.625;2.4
sim5;-87.488;-117527.1688;574904.5625;2.4
sim6;-11.1424;-183188.6846;763354;2.4"""
Now, StringIO
should solve the problem
from io import StringIO
df = pd.read_csv(StringIO(csv), sep=';')
[Out]:
Name OffsetBarrierX MeanF MaxF Thickness
0 sim1 88.4160 -120140.0402 453487.6875 2.4
1 sim2 -108.3210 -119949.1180 447437.9688 2.4
2 sim3 -89.2210 -119324.1906 576758.6250 2.4
3 sim4 -89.2210 -119324.1906 576758.6250 2.4
4 sim5 -87.4880 -117527.1688 574904.5625 2.4
5 sim6 -11.1424 -183188.6846 763354.0000 2.4
Upvotes: 1