Home > OS >  How to keep the same index and header of initial dataframe after using the (to_csv) function?
How to keep the same index and header of initial dataframe after using the (to_csv) function?

Time:07-15

I have this multi index dataframe :

            Oeil_g                   ...       Incli        O_H            O_V
               d0       d1       d2  ...                                        
face 1    25.9615  38.0526  52.0096  ...  -0.0132282    0.00612446    -0.0185327
face 2    29.8329  43.0465  56.0803  ...  -0.0259846     -0.011816    -0.0288471
face 3    26.9258  38.0132  51.0098  ...  0.00646753    -0.0194923     0.0441854
face 4    29.8329  41.0488  56.0357  ...           0    -0.0158057     0.0186262
face 5    25.0799  37.6563   50.636  ...           0    0.00874045     0.0883887

and i am trying to save it to a (.Csv) format like this :

az.to_csv("dataframe_faces.csv",header=True)

and i am getting this as result after reading the csv file:

      df=pd.read_csv("dataframe_faces.csv")
      print(df)



        Unnamed: 0              Oeil_g  ... Orientation_H Orientation_V
   0           NaN                  d0  ...        NaN            NaN
   1        face 1   25.96150997149434  ...      0.006124     -0.018533
   2        face 2  29.832867780352597  ...     -0.011816     -0.028847
   3        face 3   26.92582403567252  ...     -0.019492      0.044185
   4        face 4  29.832867780352597  ...     -0.015806      0.018626

i just wanna to keep the identical dataframe in firstplace after saving it to csv ?

Also i am trying to delete those Nan with (dropna) but it seems to delete the entire row, any idea ?

CodePudding user response:

By referencing https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

I think you should specify parameter index_col to be equal to your first column in the CSV so that face1, face2, face3 .. is still your index.

  • Related