Recipe: How to save Pandas DataFrame as CSV file?
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


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from Capgemini
1 developer from Ericsson
1 developer from Inteliwaves Technology
1 developer from Renovite Technologies
1 developer from Altimetrik
1 developer from Citi
1 developer from HvH
1 developer from Lucky Brand
1 developer from Tata Consultancy Services
1 developer from ANAC