Python is the world most lovable programming language. It provides many libraries that makes data analysis and visualization easier. There is a one library that allows you to manipulate datasets and make good analysis from it and it is Pandas library. In this tutorial you will learn how to read a csv file using pandas read_csv() function using steps.
What is CSV File ?
CSV is also know as comma separated values. It allows you to store tabular data where all the values of each column are separated by commas. Most of the data analyzer export data in CSV format as its very easy to read and write and also compatible with most of the software applications.
Sample CSV File
import pandas as pd
data = {"name":["Rob","Maya","Abhishek","Sukesh"],"age":[23,25,27,29]}
df = pd.DataFrame(data)
df.to_csv("data.csv")
The above lines of code will export the dataframe to the CSV file named “data.csv”. It is only for demonstration purpose. You can use your own dataset.
Steps to Read CSV file using Pandas
In this section you will know all the steps required to read a csv file. Just follow steps for more understanding.
Step 1: Import the required library
The first step is to import the required library. In this example I am using pandas library only. So lets import it using the import statement.
import pandas as pd
Step 2: Read the CSV File
The next step is to read the CSV File. You can read any CSV file uisng the pandas read_csv() method. It takes the path of your file as an argument and returns a Pandas DataFrame Object.
Add the below line of code to read CSV file.
df = pd.read_csv('data.csv')
Step 3: Display the data
Now after reading the datasets you can printout the data using the head() function. It allows you to display first 5 rows of the dataframe.
Use the below line of code.
print(df.head())
name | age | |
---|---|---|
0 | Rob | 23 |
1 | Maya | 25 |
2 | Abhishek | 27 |
3 | Sukesh | 29 |
Step 4: Manipulate the dataframe
There are many function for the dataframe that allows you to manipulate it. Some of these are the below.
head(n): Display first n rows
tail(n): Allows you to display the last n rows
describe(): It generates descriptive statistics.
info(): Print summary of the dataframe.
drop(columns=[‘column_name’]) : Allows you to drop a particular column.
rename(columns={‘old_name’: ‘new_name’}): You can change the name of the column using it.
Conclusion
Pandas is the best library for data manipulation. In this tutorial you have learned how to use the pandas read_csv() function to read a CSV File. You have also learned how to manipulate CSV data using the various method.
I hope you have liked this tutorial. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.