Articles → Pandas → Write In CSV File Using To_Csv Function In Pandas

Write In CSV File Using To_Csv Function In Pandas






Example


import pandas as pd

# Create a dictionary
dict = {
    "company": ["Google", "Microsoft", "Apple", "Google", "Microsoft", "Apple"],
    "year": [2017, 2017, 2017, 2018, 2018, 2018],
    "Profit": [20000, 40000, 60000, 80000, 70000, 50000],
}


# Create a dataframe
df = pd.DataFrame(dict, index=[0, 1, 2, 3, 4, 5])


df.to_csv("c:\\temp\\B.csv")



Output


Picture showing the output of using to_csv function in pandas to write data in csv file
Click to Enlarge


Index = False Attribute




import pandas as pd

# Create a dictionary
dict = {
    "company": ["Google", "Microsoft", "Apple", "Google", "Microsoft", "Apple"],
    "year": [2017, 2017, 2017, 2018, 2018, 2018],
    "Profit": [20000, 40000, 60000, 80000, 70000, 50000],
}

# Create a dataframe
df = pd.DataFrame(dict, index=[0, 1, 2, 3, 4, 5])

df.to_csv("c:\\temp\\B.csv", index=False)




Picture showing the output of using the attribute index=False
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, May 8, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250