简体   繁体   中英

Pandas Dataframe to_csv flipping column names?

Issue: Pandas appears to be swapping the column data on the data frame when it is saving to CSV? What is going on

# Code
myDF.to_csv('./myDF.csv')
print(myDF)
# Print Output
                                     dd-3             dd-4
5346177884_triplet+                   3                 3
5346177884_dublet-                    5                 5
5346177884_dublet+                    3                 3
...
1434120345_triplet+                  NaN                1
1434120345_singlet+                  NaN                3
# CSV File
,dd-3,dd-4
5346177884_triplet+,3.0,3
5346177884_dublet-,5.0,5
5346177884_dublet+,3.0,3
...
1434120345_triplet+,,1
1434120345_singlet+,,3

Anyone seen anything like this before?

Be sure to check the raw CSV file to make sure that it is not the tool you are using to display the CSV that is interpreting the file incorrectly. For instance pandas will output nans as blank space in a csv file. While libercalc on import can be set to merge repeat delimiters for things like space separated files with multiple spaces. If you accidentally leave that feature on when importing a csv with blanks between delimiters you may see an effect similar to what you have reported.

Issue:

# CSV Format 
,h1,h2,3
obj,v1,v2,v3

# PD handling NAN for v1 & v2
,h1,h2,3
obj,,,v3

# Merge delimiter interpretation
,h1,h2,h3
obj,v3

# Resulting View
                                     h1          h2          h3
obj_number                           v3                 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM