. Advertisement .
..3..
. Advertisement .
..4..
DataFrame is the tool that is being used popularly these days because of its flexibility and efficiency in data manipulation and indexing. It provides read/write functionality of data in many file formats: csv, text, excel, sql database, hdf5 and can automatically bring messy data into a structured form. In this article, we will show you how to Export Pandas DataFrame to a CSV File. Let’s follow along!
How to Export Pandas DataFrame to a CSV File
Export Pandas DataFrame to a CSV File operation is quite similar to how to import a CSV file. To be able to do this, you still have to have pandas installed on your computer first. If not, run the command pip install pandas.
And here are the step-by-step details:
- Import data as Pandas DataFrame
- Syntax declaration
- Run the command and return the result.
use the to_csv() function
To Export Pandas DataFrame to a CSV File, you will need to use the to_csv() function.
Syntax:
df.to_csv(r'Path where you want to store the exported CSV file\File Name.csv', index = False)
Returns:
The resulting CSV file will be in the form of a string or not.
In the above syntax there are other parameters you may be interested in such as:
- index: If you want indexed results, add the index parameter, otherwise you can skip it.
- header: add or not add column names.
- date_format: String format with datetime objects.
In addition, in the syntax there are many other parameters. If you are interested, check out the details here!
Example
In order to help you easily visualize and manipulate, we will give specific examples below. Assuming this is the data you currently have and need to process them:
Product | Quantity |
rose | 600 |
tulip | 500 |
peony | 800 |
lily | 900 |
Firstly, you will convert the above data to a pandas dataframe, like this:
import pandas as pd
data = {'Product': ['rose','tulip','peony','lily'],
'Quantity': [600,500,800,900]
}
df = pd.DataFrame(data, columns= ['Product', 'Quantity'])
print (df)
Then to export the file, simply declare the following syntax:
import pandas as pd
data = {'Product': ['rose','tulip','peony','lily'],
'Quantity': [600,500,800,900]
}
df = pd.DataFrame(data, columns= ['Product', 'Quantity'])
df.to_csv (r'C:\Users\Kim\Desktop\my_data.csv', index = False)
print (df)
Output
Product,Quantity
rose,600
tulip,500
peony,800
lily,900
Note: To not get the error “(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
” you must not omit the “r” character.
Alternatively, you can convert to a text file if you replace the .csv extension with .txt
Conclusion
Above is the answer for the topic “Tutorial: Export Pandas DataFrame to a CSV File” and relevant information as well. One day you are confronted with this kind of task. We trust that our method will assist you in completing your assignment quickly. Please offer your thoughts in the comment box if you have any alternative key to this question. Thank you!
Leave a comment