HomeLinuxPandas Export to CSV

Pandas Export to CSV


CSVs or comma-separated values are extraordinarily helpful codecs to type the information in outlined textual content information (normally separated by commas) and prepare the information in separate rows. They’re ceaselessly utilized in quite a few functions on account of their interoperability which allows you to simply transfer the information between a number of codecs.

Pandas to_csv() exports your DataFrame as a comma-separated worth (CSV) datatype to your machine. This implies you could return your knowledge everytime you need. To make the most of this methodology, now we have to observe the next supplied syntax:

Syntax:

 

pandas.DataFrame_object.to_csv(“file_name.csv”,index=True,header=True,columns,index_
label=None)

 

Parameters:

    1. The primary parameter is the identify of the CSV file.
    2. By default, the index is True. If we specify False, the row indices are eliminated in CSV.
    3. By default, the header is True. If we specify False, the columns are eliminated in CSV.
    4. If you wish to convert solely the actual columns of DataFrame to CSV, it’s good to go these columns to the columns parameter in an inventory.
    5. The index_label takes an current column and units this column as an index within the CSV file.

Instance 1: Passing Solely the Paramater

Create a DataFrame with 5 columns and 5 information. Convert it right into a CSV file by passing solely the file_name as a parameter.

import pandas

# Take into account the DataFrame having 5 columns
program=pandas.DataFrame({‘supervisor id’:[1,2,3,4,5],
                          ‘identify’:[‘pill’,‘dee dee’,‘ghorak’,‘teon’,‘marky’],
                        ‘Concept’:[‘House drainage’,‘All’,‘water supply’,‘electricity’,‘drilling’],
                        ‘demography’:[‘ap’,‘gujarat’,‘patna’,‘indore’,‘norway’]})

print(program)

# Export the above DataFrame to the csv file.
program.to_csv(“Program.csv”)

print(‘Exported to CSV..’)

 
Output:

   supervisor id     identify            Concept demography
0           1     capsule  Home drainage         ap
1           2  dee dee             All    gujarat
2           3   ghorak    water provide      patna
3           4     teon     electrical energy     indore
4           5    marky        drilling     norway
Exported to CSV..

 
The file_name that we go is “Program.csv”. If you open the file, you may see that the complete DataFrame is transformed to a CSV file.

Instance 2: With out the Index

 

Let’s convert the earlier DataFrame to CSV by ignoring the index. To do that, set the index parameter to False.

import pandas

# Take into account the DataFrame having 5 columns
program=pandas.DataFrame({‘supervisor id’:[1,2,3,4,5],
                          ‘identify’:[‘pill’,‘dee dee’,‘ghorak’,‘teon’,‘marky’],
                        ‘Concept’:[‘House drainage’,‘All’,‘water supply’,‘electricity’,‘drilling’],
                        ‘demography’:[‘ap’,‘gujarat’,‘patna’,‘indore’,‘norway’]})

# Export the above DataFrame to the csv file with out index
program.to_csv(“Program.csv”,index=False)

print(‘Exported to CSV with out Index..’)

 
Output:

Exported to CSV with out Index..

 
The file_name that we go is “Program.csv”. If you open the file, you may see that the complete DataFrame is transformed to a CSV file with out indices.

Instance 3: With out the Header

Let’s convert the earlier DataFrame to CSV by ignoring the column. To do that, set the header parameter to False.

import pandas

# Take into account the DataFrame having 5 columns
program=pandas.DataFrame({‘supervisor id’:[1,2,3,4,5],
                          ‘identify’:[‘pill’,‘dee dee’,‘ghorak’,‘teon’,‘marky’],
                        ‘Concept’:[‘House drainage’,‘All’,‘water supply’,‘electricity’,‘drilling’],
                        ‘demography’:[‘ap’,‘gujarat’,‘patna’,‘indore’,‘norway’]})

# Export the above DataFrame to the csv file with out header
program.to_csv(“Program.csv”,header=False)

print(‘Exported to CSV with out header..’)

 
Output:

Exported to CSV with out header..

 
The file_name that we go is “Program.csv”. If you open the file, you may see that the complete DataFrame is transformed to a CSV file with out column names.

Instance 4: Explicit Columns to CSV

To this point, now we have seen learn how to convert the complete DataFrame to CSV. Now, we convert solely the actual columns within the current DataFrame to a CSV file.

import pandas

# Take into account the DataFrame having 5 columns
program=pandas.DataFrame({‘supervisor id’:[1,2,3,4,5],
                          ‘identify’:[‘pill’,‘dee dee’,‘ghorak’,‘teon’,‘marky’],
                        ‘Concept’:[‘House drainage’,‘All’,‘water supply’,‘electricity’,‘drilling’],
                        ‘demography’:[‘ap’,‘gujarat’,‘patna’,‘indore’,‘norway’]})

# Export solely 2 column within the above DataFrame to the csv
program.to_csv(“Program.csv”,columns=[‘name’,‘Idea’])

print(‘Exported to CSV..’)

 
Output:

 
The file_name that we go is “Program.csv”. If you open the file, you may see that solely the “identify” and “Concept” columns within the DataFrame are transformed to a CSV file.

Instance 5: With Index_Label

Set the “supervisor id” column as index within the transformed CSV file by setting the “supervisor id” column to the index_label.

import pandas

# Take into account the DataFrame having 5 columns
program=pandas.DataFrame({‘supervisor id’:[1,2,3,4,5],
                          ‘identify’:[‘pill’,‘dee dee’,‘ghorak’,‘teon’,‘marky’],
                        ‘Concept’:[‘House drainage’,‘All’,‘water supply’,‘electricity’,‘drilling’],
                        ‘demography’:[‘ap’,‘gujarat’,‘patna’,‘indore’,‘norway’]})

# Export above DataFrame to csv file by setting ‘supervisor id’ column as index.
program.to_csv(“Program.csv”,index_label=‘supervisor id’)

print(‘Exported to CSV with index – supervisor id.’)

 
Output:

Exported to CSV with index – supervisor id.

 
You may see that the “supervisor id” column is ready to index.

Conclusion

Exporting a Pandas DataFrame right into a CSV file is a really helpful apply. On this writing, we briefly knowledgeable you concerning the CSV information and defined the “to_csv()” Pandas methodology which is used to export the DataFrame to CSV information. The syntax with all of the 5 parameters is defined on this information with an instance.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments