pythonpandasdataframe

How to specify location where Pandas to CSV file is stored in my directory


So I do not know how to save the csv which this code creates to a specific folder in my directory. Any help would be appreciated!

#Store rows which do not conform to the relationship in a new dataframe
subset = df[df['check_total_relationship'] == False]`

subset.to_csv('false_relationships.csv', index=False, header=True, encoding='utf-8')`

Solution

  • Just specify the folder in your directory.

    subset.to_csv('output_data/false_relationships.csv', index=False, header=True, encoding='utf-8')
    

    Or you can specify the absolute path

    subset.to_csv('/absolute-path/output_data/false_relationships.csv', index=False, header=True, encoding='utf-8')
    

    If you need to join paths you can use os.path

    import os
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    
    filepath = 'files/one.txt'
    request_path = os.path.join(BASE_DIR, filepath)