Welcome to the 7 days of Pandas challenge!
In this series, we'll cover the basics and the commonly used operations in pandas
library in Python which is primarily used for data manipulation.
In pandas
, the main object we use is a DataFrame
which is an object that stores the data into a tabular form and lets us perform operations on it.
Day 1 - Read and Write Data from CSV files using pandas
In this article, we will cover how to load data from a CSV file into a dataframe and then write a dataframe to a CSV file using the pandas
module.
Read data from CSV file in pandas
You can use the pandas.read_csv()
function to read data from a CSV file into a dataframe. The following is the syntax -
import pandas as pd
# read data from csv file
df = pd.read_csv(PATH_TO_FILE)
Pass the path to the CSV file as an argument to the pandas.read_csv()
function. It reads the data from the CSV file and returns the resulting dataframe with that data.
Let's look at an example.
We'll read the data from a file called "Pokemon.csv" saved in the current working directory as a dataframe.
# import the pandas module
import pandas as pd
# read data from csv file
df = pd.read_csv("Pokemon.csv")
# display the first five rows of the dataframe
df.head()
Output:
You can see that the data from the CSV file was loaded in the dataframe. Now, you can go ahead and analyze/manipulate the data as per your requirements.
Write data to a CSV file using pandas
You can also use the pandas
module to save a dataframe as a CSV file. For example, after working with and changing the data in a dataframe, you may want to save it for later use.
Use the pandas.DataFrame.to_csv()
function to save a pandas dataframe as a CSV file. The following is the syntax -
# save dataframe to a csv file
df.to_csv(PATH_TO_NEW_FILE)
Pass the path (or just the file name in case you want to save the dataframe as csv in the current working directory) as an argument to the pandas.DataFrame.to_csv()
function.
Note that, if you do not want the dataframe index as an additional column in the resulting CSV file, pass index=False
as an argument.
Let's look at an example.
Let's write the above dataframe df
to a new CSV file called "Pokemon2.csv".
# write dataframe to a csv file
df.to_csv("Pokemon2.csv", index=False)
If you open the CSV file, it looks something like this -
You can see that the data was successfully written to the CSV file.
That'll be it for this article. In the coming articles, we will dive deep into using pandas and some of its most powerful and useful functionalities.
Top comments (0)