Pandas - Read and Write Data From CSV files - 7 Days of Pandas
Tags: python, pandas, datascience
Published: 2022-12-21 12:11:41

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 -

```python
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.

```python
# 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:


![first five rows of the dataframe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ia4tsq716d4srgew0hp.png)


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 -

```python
# 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".

```python
# write dataframe to a csv file
df.to_csv("Pokemon2.csv", index=False)
```

If you open the CSV file, it looks something like this -

![the resulting csv file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atjblhs0b0gvet9s6bs2.png)

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.

## References

- [Read CSV files using Pandas – With Examples](https://datascienceparichay.com/article/read-csv-files-using-pandas-with-examples/)
- [`pandas.read_csv()` docs] (https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)
- [`pandas.DataFrame.to_csv()` docs](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html#pandas.DataFrame.to_csv)