Pandas dataframe transpose, to_csv
Asked Answered
A

2

5

In the code below, in line 4 I can transpose the dataframe, but in line 5, when I use to_csv, the new CSV file is created, it remains the original version and not the transposed one. What might have gone wrong?

import numpy as np
import pandas as pd

df = pd.read_csv('~/N.csv')

df2 = df.T

df2 = df.to_csv('~/N_transposed.csv')

Thank you!

Acadian answered 7/4, 2014 at 22:50 Comment(1)
Why do you expect to get back a dataframe from the df.to_csv() command?Kinfolk
F
8

No need to use df2 =

This is enough..

df2.to_csv('~/N_transposed.csv')
Fauna answered 7/4, 2014 at 23:57 Comment(0)
C
4

In line 5, use

df3 = df2.to_csv('~/N_transposed.csv') 

or

 df2.to_csv('~/N_transposed.csv') 

The df variable has not been altered, the result is stored in df2 and that's what you need to output to csv, not df.to_csv.

Cornellcornelle answered 7/4, 2014 at 22:53 Comment(1)
@ap235711 Well I see you've already accepted another answer. Cool then ☺Cornellcornelle

© 2022 - 2024 — McMap. All rights reserved.