Python Pandas CSV to Dataframe
In this post, you will learn the simple process to read a csv file and convert to a Python pandas dataframe.
CSV (Comma-separated-values) is widely used for data exchange in data-based applications, like- business, consumer, scientific applications. In web applications, we generally store the data in arrays, dataframes, lists, tuples, dictionaries, etc. There may be a need for clients to export this data to a CSV file. The data in CSV is stored as sequences of records. Like in other programming languages, we can also write data to a CSV file.
Pandas CSV to dataframe using read_csv() method
Python pandas provides read_csv() method to read a csv file. It has so many attributes, but here we have mentioned only the mostly used attributes-
read_csv(filepath, sep, header, names, index_col, usecols, squeeze,
prefix, dtype, skipinitialspace, skiprows, skipfooter, nrows, skip_blank_lines, compression)
filepath - any valid csv file path.
sep - the string delimiter of length 1.
header - the row number(s) to use as the column names, and the start of the data.
names - list of column names to use.
index_col(int) - Column(s) to use as the row labels of the DataFrame, either given as string name or column index.
usecols - optional, returns a subset of the columns.
squeeze - default false, return a series in case the parsed data contains only one column.
prefix (str) - optional, to add column numbers when no headers.
dtype - optional, columns data type.
skipinitialspace - optional, default false, skips space after delimiter.
skiprows(int) - optional, the number of lines to skip.
skipfooter(int) - optional, the number of lines at the bottom of file to skip.
nrows(int) - optional, number of rows of file to read.
skip_blank_lines - default True, skip over blank lines rather than interpreting as NaN values.
compression - represents the compression mode, the possible values are 'infer', 'gzip', 'bz2', 'zip', 'xz', None and default is 'infer'.
Python CSV to dataframe program1
Here is the simple code to read a csv file and return data.
import pandas as pd
# csv to dataframes
df = pd.read_csv('highest_score.csv')
print(df)
Once you run the above code, it parses and returns the data.Python csv to dataframe program2
In the read_csv() method, we can specify the column names using the names attribute.
import pandas as pd
# csv to dataframes
df = pd.read_csv('highest_score.csv', names=['Name','Sub','Marks'])
print(df)
Once you run the above code, it parses and returns the data.Python CSV to dataframe program3
Here, we have used the 'header' attribute and set the row number to start the data.
import pandas as pd
# csv to dataframes
df = pd.read_csv('highest_score.csv',
names=['Name','Subject','Marks'], header=1)
print(df)
Once you run the above code, it parses and returns the data.Python CSV to dataframe program4
Here, we have used the 'sep' attribute.
import pandas as pd
# csv to dataframes
df = pd.read_csv('highest_score.csv', sep=":")
print(df)
Once you run the above code, it parses and returns the data.Pandas Convert CSV to DataFrame5
The following code load a dataframe from a csv file.
import pandas as pd
# csv to dataframes
df = pd.read_csv('highest_score.csv', header=0,
index_col=0, quotechar='"',sep=':',
na_values = ['na', '-', '.', ''])
print(df)
Once you run the above code, it parses and returns the data.Columns: []
Index: [Alisa,Eng,95, Alya,Maths,98, Kim,Sci,89, Gaga,GK,93, Smith,Phy,90, Priska,Maths,99, Kashyap,Chm,92, Andy,Bio,89]
Related Articles
Pandas string to datetimeFillna Pandas Example
Convert Excel to CSV Python Pandas
Python Pandas CSV to Dataframe
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python Tkinter Geometry Managers
Python Tkinter Scale Widget
Python Tkinter Overview and Examples
Python Turtle Graphics Overview
Factorial Program in Python
Python snake game code with Pygame
Python JSON Tutorial - Create, Read, Parse JSON file
Python convert xml to dict
Python convert dict to xml