Fiddelush
Fiddelush

Reputation: 43

From a string to Pandas DataFrame (Python)

I have a CSV file containing a string of numbers like this:

1.0,2.0,1.0,1.0,1.0,2.0....

I want to read it in but make a new row for each ",". Can't find how to do that while using pd.read_csv, any suggestions?

Thank you!

Upvotes: 0

Views: 65

Answers (2)

mozway
mozway

Reputation: 260640

You can read_csv without header and transpose the dataframe:

pd.read_csv('yourfile.csv', header=None).T

output:

     0
0  1.0
1  2.0
2  1.0
3  1.0
4  1.0
5  2.0

Transposing a similar to Matrix Transpose where you switch rows with columns. So all your columns become rows and rows become columns.

In the code above, We're first reading the csv file as one row but then we are calling .T which is transposing all columns as rows.

Upvotes: 2

Tom Ron
Tom Ron

Reputation: 6181

You can use the lineterminator parameter. The following command will load the csv below which has no header, fields are separated by . and lines are separated by commas (,).

pd.read_csv("my_csv.csv", sep=".", header=None, lineterminator=",")

CSV -

1.1,2.2

Upvotes: 1

Related Questions