Comprehensive not import export data csv to dataframes

Import export data csv to dataframes is an important part of python syllabus. Whenever you are working with python you need to save the dataframe data from output screen to file and vice-versa. I have already written a detailed post about Data files introduction. Click here to read

import export data csv to dataframes

Let’s start with import export data csv to dataframes with exporting data from dataframe to CSV files. 
Consider following example: 
import pandas as pd
emp_dict = {'Name':['Sagar','Mohit','Arjun','Manav','Malayketu'],
f = open('D:\mydata.csv','r')
data =

In above example 

  • Employees data stored in a dictionary : emp_dict
  • DataFrame created using pandas: df
  • Data exported into mydata.csv file using to_csv() function: df.to_csv()
  • CSV file opened through open() function with read mode: f = open(‘mydata.csv’,’r’)
  • Data read by function read(): data =
  • Data printed using print function : priint(data)
  • The opened needs to be closed to avoid malfunctioning in csv file: f.close()

Now have look at recommended options with to_csv() for import export data csv to dataframes.

Recommended Options with to_csv() functions:
  1. path_or_buf: This argument receives a file or string buffer. If path is nor provided as a parameter it will save data in CSV format. User can provide absolute or relative path. In above example relative path is given to the file. 
    Exporting Data into absolute path
  2. sep: It specifies the separator character to separate the data columns. By default it is comma. In below example ‘|’ symbol is used to separate data.
    Data export using Separator 
  3. na_rep:It specifies the value in place of NaN. The default is ”.
    na_rep example
  4. float_format: This option specifies the number format to store in CSV file. As you know python displays a large number after decimal values in output. So this option reduce the length of digits into specified digits. 
    use of float_format
  5. header: It is used to export data column header into CSV. It can be specified True or False. By default it is True.
    header as parameter in to_csv
  6. columns: To write columns into CSV. By default it is None.
    Displaying two columns only
  7. index: To write row number or not. By default it is True.
    Using index Parameter

Exporting Data into text files:

Data can be exported using simple writing operation into text file. To write data into data frame to text file follow these steps:
  1. Create a dataframe.
  2. Create a text file with ‘w’ mode.
  3. Convert data into str using str() function and use write function.
  4. Read data to check the output. 
    Export data into text file using write operation

The next subtopic of import export data csv to dataframes is import data through file.

Import Data through files:

To import data read_csv function is used. It store the values from different files.
Consider this example:
Importing data from file
A read_csv() function contains following commonly used parameters: 
  1. file_path or buffer: It is similar as to_csv() parameter.
  2. sep: It is too similar to to_csv() sep parameter.
  3. index_col: Make a passed column as an index
    Using index_col in read_csv() function
  4.  Header: Change the header of as passed row
    Header in read_csv

I hope you enjoyed import export data csv to dataframes. Hit the like button and share this article with your friends.

Comment Your Views

%d bloggers like this: