How To Manipulate csv, xlsx, and json Data in Python Using Pandas

Nick McCullum Nick McCullum ⏰ 4 Minutes 📅 Apr 17, 2020

Python is one of the best languages for working with large datasets. Data scientists around the world use it for both exploratory and descriptive data projects.

With that said, Python itself lacks many of the core capabilities that data scientists require. We instead rely on an outside programming library called pandas.

In this tutorial, I will show you how to manipulate csv, xlsx, and json data in Python using the pandas programming library.

Installing Pandas

To manipulate data using the pandas programming library, you’ll first need to import pandas into your Python script. Here’s the command you would use to import pandas using the alias pd (which is convention in the data science world):

import pandas as pd

If you encounter an error when executing this command, then it is likely that you do not have pandas installed on your machine. You can install pandas easily using the pip package manager by running the following on the command line:

$ pip install pandas

Data Input and Output in Pandas

Pandas includes methods for inputting and outputting data from its DataFrame object. There are different methods for csv, xlsx, and json files, but they all follow similar syntax. Data input methods look like read_filetype(path) while data output methods look like to_filetype(path).

As an example, let’s use a data set of stock prices that I have uploaded to a public GitHub repository. The path to this database (for the .csv file) is Simply replace the .csv on the end with .xlsx and .json for equivalent databases stored in the other file types.

Now that we have a data set to import, lets see how we would import and save this file:

#Import the csv data as a pandas DataFrame

csv_data = pd.read_csv('')

#Save the DataFrame as a file on our local computer


#Import the xlsx data as a pandas DataFrame

excel_data = pd.read_excel('')

#Save the DataFrame as a file on our local computer


#Import the json data as a pandas DataFrame

json_data = pd.read_json('')

#Save the DataFrame as a file on our local computer


Pandas Core Functionality

Now that we have imported (and saved) the data as a pandas DataFrame, let’s consider some of the core functionality included in the pandas Python library.

How To Create Pandas Series From Pandas DataFrames

The pandas library has another data structure called a pandas Series which is very similar to a NumPy array. It is a one-dimensional list of data elements.

You can create a pandas Series that contains the data from a row of a pandas DataFrame by referencing the DataFrame’s variable name and passing in the column name in square brackets.

As an example, the csv_data DataFrame has a column called Alphabet Inc Price. Here’s how you would create a Series using the data from this column:

my_series = csv_data['Alphabet Inc Price']

How To Deal With Missing Data in a Pandas DataFrame

It is common to encounter missing data when dealing with large datasets. Fortunately, the pandas library has built-in methods that make it easy to work with missing data.

The first method is dropna, which removes any rows that have missing data in the DataFrame. Here’s how you would apply the dropna method to the csv_data DataFrame:


You can also pass in the axis=1 argument into the dropna method to force the method to exclude columns with missing data instead of rows with missing data, like this:


In certain cases, it is useful to replace missing data instead of excluding it entirely. The fillna method solves this problem.

As an example, here’s how you would replace all of the missing data in csv_data with 0:


It is common to nest the mean method within the filna method to replace missing values with the average value of a DataFrame (or even a row or column from that DataFrame).

How To Sort Pandas DataFrames

It is possible to sort a pandas DataFrame using the sort_values method. This is similar to applying a filter in Excel.

As an example, the csv_data DataFrame has a column called Inc Price. Here’s how you would sort the DataFrame by this column in ascending order:

csv_data.sort_values(' Inc Price')

Final Thoughts

Manipulating data that is stored in csv, xlsx, and json files is an important concept for any data scientist to understand.

In this tutorial, you learned:

  • How to import pandas
  • How to import datasets stored in remote locations into a pandas DataFrame by pinging their URL path
  • How to create a pandas Series from a pandas DataFrame
  • How to deal with missing data in Pandas
  • How to sort pandas DataFrames

Please feel free to refer back to this tutorial if you ever get stuck in the future!

Further Reading

If you enjoyed this article by Nick, you may also enjoy some of the other articles on the site: