Reputation: 25
Hello I am trying to make one csv from another csv file.
Below is my input file in csv.
1 2
3 4
5 6
And my desired output like
1
2
3
4
5
6
Basically I am trying to convert each row into column.
csvfile = open('main.csv', 'r')
textcsv = csv.reader(csvfile, delimiter=',')
for row in textcsv:
list_ = list(row)
column = pd.DataFrame(list_)
outputDF = pd.concat([outputDF, column], axis=1)
outputDF.to_csv(output.csv, sep=',', index=False)
I got the output which convert row in column but not repeated.
Upvotes: 0
Views: 192
Reputation: 62373
pd.DataFrame.stack
:df = pd.read_csv('test.csv')
print(df)
df2 = pd.DataFrame(df.stack())
df2.reset_index(inplace=True, drop=True)
print(df2)
df2.to_csv('output.csv', sep=',', index=False)
Upvotes: 2