DATA MUNGING PYTHON PANDAS DATAFRAME PANDAS CHEATSHEET PANDAS DATAFRAME TUTORIAL

How to save Pandas DataFrame as CSV file?

This recipe helps you save Pandas DataFrame as CSV file
In [1]:
## How to save Pandas DataFrame as CSV file
def Snippet_105():
    print()
    print(format('How to save Pandas DataFrame as CSV file','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create dataframe
    raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'age': [42, 52, 36, 24, 73],
                'preTestScore': [4, 24, 31, 2, 3],
                'postTestScore': [25, 94, 57, 62, 70]}

    df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])
    print(); print(df)

    # save the dataframe
    df.to_csv('raw_data.csv', encoding='utf-8', index=False)
    df.to_excel('raw_data.xls', encoding='utf-8', index=False)

Snippet_105()
*********************How to save Pandas DataFrame as CSV file*********************

  first_name last_name  age  preTestScore  postTestScore
0      Jason    Miller   42             4             25
1      Molly  Jacobson   52            24             94
2       Tina       Ali   36            31             57
3       Jake    Milner   24             2             62
4        Amy     Cooze   73             3             70


Companies using this Recipe
1 employee of Altimetrik
1 employee of ICU Medical
1 employee of ANAC
1 employee of Renovite Technologies
1 employee of eClerx
1 employee of Ericsson