How to Import CSV and XLS Data into Pandas
Tags: python, beginners
Published: 2017-11-26 16:44:07

---
title: How to Import CSV and XLS Data into Pandas
published: true
description:  A quick guide to importing data into Pandas.
tags: Python, beginner
---

![Pandas](https://thepracticaldev.s3.amazonaws.com/i/k6n0dfj5z0zz9l1fbht5.jpg)
[Pandas](http://pandas.pydata.org/) is a Python Data Analysis Library. It allows you to play around with data and perform powerful data analysis.

In this example I will show you how to read data from CSV and Excel files in Pandas. You can then save the read output as in a Pandas dataframe. The sample data used in the below exercise was generated by https://mockaroo.com/.


```python
import pandas as pd
```


```python
csv_data_df = pd.read_csv('data/MOCK_DATA.csv')
```

Preview the first 5 lines of the data with `.head()` to ensure that it loaded.


```python
csv_data_df.head()
```




<div>
<style>
    .dataframe thead tr:only-child th {
        text-align: right;
    }

    .dataframe thead th {
        text-align: left;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>id</th>
      <th>first_name</th>
      <th>last_name</th>
      <th>email</th>
      <th>gender</th>
      <th>ip_address</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>Ross</td>
      <td>Ricart</td>
      <td>rricart0@berkeley.edu</td>
      <td>Male</td>
      <td>217.151.154.186</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>Jenn</td>
      <td>Pizer</td>
      <td>jpizer1@usnews.com</td>
      <td>Female</td>
      <td>104.123.13.234</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>Delainey</td>
      <td>Sulley</td>
      <td>dsulley2@xing.com</td>
      <td>Male</td>
      <td>6.101.0.150</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>Nessie</td>
      <td>Feirn</td>
      <td>nfeirn3@samsung.com</td>
      <td>Female</td>
      <td>97.93.173.170</td>
    </tr>
    <tr>
      <th>4</th>
      <td>5</td>
      <td>Noami</td>
      <td>Flanner</td>
      <td>nflanner4@woothemes.com</td>
      <td>Female</td>
      <td>174.228.138.242</td>
    </tr>
  </tbody>
</table>
</div>



You will need to `pip install xlrd` if you haven't already. In order to import data from Excel.


```python
import xlrd
excel_data_df = pd.read_excel('data/MOCK_DATA.xlsx')
```


```python
excel_data_df.head()
```




<div>
<style>
    .dataframe thead tr:only-child th {
        text-align: right;
    }

    .dataframe thead th {
        text-align: left;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>id</th>
      <th>first_name</th>
      <th>last_name</th>
      <th>email</th>
      <th>gender</th>
      <th>ip_address</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>Chloris</td>
      <td>Antliff</td>
      <td>cantliff0@shareasale.com</td>
      <td>Female</td>
      <td>131.17.2.171</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>Brion</td>
      <td>Gierok</td>
      <td>bgierok1@posterous.com</td>
      <td>Male</td>
      <td>245.41.126.3</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>Fleur</td>
      <td>Skells</td>
      <td>fskells2@creativecommons.org</td>
      <td>Female</td>
      <td>75.0.34.132</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>Dora</td>
      <td>Privost</td>
      <td>dprivost3@newsvine.com</td>
      <td>Female</td>
      <td>51.202.4.39</td>
    </tr>
    <tr>
      <th>4</th>
      <td>5</td>
      <td>Annabella</td>
      <td>Hucker</td>
      <td>ahucker4@typepad.com</td>
      <td>Female</td>
      <td>124.80.181.41</td>
    </tr>
  </tbody>
</table>
</div>



Image Courtesy of jballeis (Own work) [CC BY-SA 3.0](http://creativecommons.org/licenses/by-sa/3.0), via Wikimedia Commons

_A version of this article was originally published by Monica Powell on [www.datalogues.com](http://www.datalogues.com/)_