eetiaro
eetiaro

Reputation: 352

Pandas: How to transpose a row to a column?

I have a csv file that I get from a specific software. In the csv file there are 196 rows, each row has a different amount of values. The values are seperated by a semicolon.

I want to have all values of the dataframe in one column, how to do it?

dftest = pd.read_csv("test.csv", sep=';', header=None)
dftest
    0
0   14,0;14,0;13,9;13,9;13,8;14,0;13,9;13,9;13,8;1...
1   14,0;14,0;13,9;14,0;14,0;13,9;14,0;14,0;13,8;1...
2   13,8;13,9;14,0;13,9;13,9;14,6;14,0;14,0;13,9;1...
3   14,5;14,4;14,2;14,1;13,9;14,1;14,1;14,2;14,1;1...
4   14,1;14,0;14,1;14,2;14,0;14,3;13,9;14,2;13,7;1...
5   14,5;14,1;14,1;14,1;14,5;14,1;13,9;14,0;14,1;1...
6   14,1;14,7;14,0;13,9;14,2;13,8;13,8;13,9;14,8;1...
7   14,7;13,9;14,2;14,7;15,0;14,5;14,0;14,3;14,0;1...
8   13,9;13,8;15,1;14,1;13,8;14,3;14,1;14,8;14,0;1...
9   15,0;14,4;14,4;13,7;15,0;13,8;14,1;15,0;15,0;1...
10  14,3;13,8;13,9;14,8;14,3;14,0;14,5;14,1;14,0;1...
11  14,5;15,5;14,0;14,1;14,0;13,8;14,2;14,0;15,9;1...

The output looks like this, I want to have all values in one column

I would like to make it look like this:

0 14,0
1 14,0
2 13,9
.
.
.

Upvotes: 1

Views: 425

Answers (2)

MikeCode
MikeCode

Reputation: 91

you can also use numpy ravel and convert this to 1D Array.

df = pd.read_csv("test.csv", sep=';', header=None)
df = pd.DataFrame(df.values.ravel(), columns=['Name'])

Upvotes: 0

jezrael
jezrael

Reputation: 862641

If there is only one column 0 with values splitted by ; use Series.str.split with DataFrame.stack:

df = dftest[0].str.split(';', expand=True).stack().reset_index(drop=True)

Upvotes: 2

Related Questions