0% found this document useful (0 votes)
12 views11 pages

CSV (Rajib)

Importing/exporting data between CSV files involves reading data from CSV files into a Pandas dataframe and writing dataframe data out to CSV files. CSV files store tabular data with comma-separated values where each row is a data record and columns are separated by commas. When reading CSV files into a dataframe, you can specify column names, the number of rows to read, and customize the separator character for files with non-comma separators. When writing a dataframe to a CSV file, you can specify the column separator and handle missing (NaN) values.

Uploaded by

Shshank Shekher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

CSV (Rajib)

Importing/exporting data between CSV files involves reading data from CSV files into a Pandas dataframe and writing dataframe data out to CSV files. CSV files store tabular data with comma-separated values where each row is a data record and columns are separated by commas. When reading CSV files into a dataframe, you can specify column names, the number of rows to read, and customize the separator character for files with non-comma separators. When writing a dataframe to a CSV file, you can specify the column separator and handle missing (NaN) values.

Uploaded by

Shshank Shekher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Importing/exporting

data
between

csv files

RAJIB CHAKRABORTY
The acronym CSV is short for Comma-Separated Values. The CSV
format refers to a tabular data that has been saved as plaintext where
data is separated by commas. For example consider a table having
some data as shown below. If you store this table’s data in CSV format
then the data of the table will be stored in CSV format as shown below.

Roll No. Name Marks Roll No.,Name,Marks


101 Tia 67.8 When converted to CSV format 101,Tia,67.8
102,Radha,78.9
102 Radha 78.9

Tabular data CSV data


Reading from a CSV file to Dataframe
You can use read_csv( ) function to read data from a CSV file in your
dataframe.
Example :

Output:
Reading CSV file and Specifying Own Column Names
Sometimes you may have a CSV file that does not have top row
containing column headers (as shown below).

Now if you read such a file, it will take the top row as the column
headers.
Example : Output:

See, it has taken


first row (which
is actually data)
as column
headings.
Reading CSV file and Specifying Own Column Names
For such a situation, you can specify own column headings in
read_csv().

Example :

Output:
This time it has not taken first row
as headings. First row remains data
and the column headings are as per
names parameters sequence.
Reading specified number of Rows from CSV file
You can read the specified number of rows from the CSV file by giving
nrows argument.

Example :

Output:
Because of argument nrows = 2,
only two rows have been read from
CSV file.
Reading from CSV files having different Separator from comma

Some CSV files are so created that their separator character is


different from comma such as a semicolon(;) or a pipe symbol(|) etc. To
read data from such CSV files, you need to specify additional argument
as sep = < separator character > .

Example :

Output:
Storing Dataframe’s Data to CSV File
Sometimes, we have data available in dataframes and we want to save
that data in a CSV file. For this purpose Python provides to_csv( )
function.
Example : DataFrame ( df )
Storing Dataframe’s Data to CSV File with separator as ‘|’

We can save the dataframe’s data to a CSV file with separator as ‘|’.

Example : DataFrame ( df )
Handling NaN Values with to_csv( )
Sometimes, your dataframe contains missing (NaN) values.
Example : DataFrame df with NaN values

Now, if you store this dataframe in a CSV file by giving the following
command :

The stud2.csv file will look like as shown below.

The NaN values are stored as


empty strings by default
Handling NaN Values with to_csv( )
You can specify your own string that can be written for NaN values in
CSV file.. DataFrame df with NaN values

Example :
The stud3.csv file will look like as shown below.

The NaN values are stored as


NULL this time because of
argument na_rep=‘NULL’ .

You might also like