Search code examples
pythondata-cleaning

How to export a cleaned dataframe to csv?


I am attempting to export a cleaned dataframe to a csv file for it to be imported into a new jupyter notebook for ML purposes.

I ran the following code: df_train.to_csv('../datasets/new_train.csv')

However, the output file, when opened, contains the same dataset as the input file (before cleaning).


Solution

  • Can you provide a minimal repeatable example, based on comments it is not clear on the mistake. Here is the sample code to prove it works

    # Import pandas library
    import pandas as pd
    data = [['tom', 10], ['nick', 15], ['juli', 14], ['jerry', ]]
    df = pd.DataFrame(data, columns=['Name', 'Age'])
    print(df)
    df.dropna(subset=['Age'], inplace=True)
    print(df)
    df.to_csv('names.csv')
    

    Output of above code

        Name   Age
    0    tom  10.0
    1   nick  15.0
    2   juli  14.0
    3  jerry   NaN
       Name   Age
    0   tom  10.0
    1  nick  15.0
    2  juli  14.0
    

    Output of cat names.csv

    ,Name,Age
    0,tom,10.0
    1,nick,15.0
    2,juli,14.0