EXPORT DATAFRAME TO EXCEL, CSV FILE IN Python PANDAS
Create a new dataframe from CSV file
The first step towards converting a pandas dataframe to CSV is to create a new dataframe from a CSV file. There are many ways to do this, but the most straightforward is to use pandas’ read_csv() function. For example, if you have a CSV file named sales.csv with the following content, you can create a new dataframe from it by running the following code.
pd.read_csv('sales.csv', header=None, index_col=False)
pd.read_csv('sales.csv', header=None, index_col=False) Here, header=None specifies that you don’t want the first row to be treated as a header and index_col=False specifies that you don’t want the columns to be treated as an index. You can, of course, specify the columns as the index if you prefer.
Convert Pandas Dataframe to Comma Separated Value (CSV)
Next, let’s see how to convert a pandas dataframe to CSV. The pandas-convert command is a simple utility that can be used to convert dataframes and other pandas objects to various different formats. Let’s see how we can do this. First, import the pandas-convert utility and convert your pandas dataframe to a CSV file with one line of code. pd.convert.to_csv(df, 'sales.csv', index=False) Here, df is the dataframe whose pandas dataframe to CSV you want to convert, sales.csv is the name of the output file, and index=False specifies that you don’t want the columns to be treated as an index.
Convert Pandas Dataframe to CSV With Index and Columns
The pandas-convert utility can also be used to convert pandas dataframes to CSV with index and columns. However, for this to work, the columns must have proper names. You can assign names to the columns by using the assign() function on the dataframe whose columns you want to assign names to. For example, the following code assigns names to the first five columns of the dataframe named df. df.assign(columns=list('AB')) Once you have assigned names to the columns, you can convert pandas dataframe to CSV with index and columns using the following code. pd.convert.to_csv(df, 'sales.csv', index=True)
Share, Support, Subscribe!!!
YouTube: https://www.youtube.com/c/xpertji?sub...
Facebook: / xpertji
Twitter: / xpertji
Website : https://xpertji.com
patreon : / xpertji