Reading csv dataset and explore the data
Tags: python, pandas, tips
Published: 2022-02-10 21:02:10

First of all, we are going to use [Meteorite_Landings.csv](https://data.nasa.gov/api/views/gh4g-9sfh/rows.csv?accessType=DOWNLOAD) dataset from **NASA.**

Once downloaded, let's jump into the code! 🚀

```python
import pandas as pd

# Loading csv into our dataframe
meterorites_df = pd.read_csv('dir/to/csv_file')
```

Using pandas allow us to use different methods to understand the dataset. For example we can see the shape of the dataframe ↔️

```python
df_meteorites.shape
```
`(45716, 10)`

Using **head()** will show us the first 5 rows of the dataframe 💆‍♂️
```python
df_meteorites.head()
```
![df_meteorites.head()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n65c68z26kdkqzais2rv.png)

And also yo can set to **head(n)** how many rows you want to see

```python
df_meteorites.head(15)
```
![df_meteorites.head(15)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrygwcybk4crr3j1k6mc.png)
 
We can see the last rows as well using **tail()** it works the same way as **head()**

```python
df_meteorites.tail()
```
![df_meteorites.tail()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdyr4p0hd98uwb2hiqxe.png)
```python
df_meteorites.tail(15)
```
![df_meteorites.tail(15)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk37hi6jbj5m2vidideq.png)

And now my favorite way to see the data is by using **sample(n)**

```python
df_meteorites.sample(20)
```
This method will randomly get a sample of rows from our dataframe. I found this useful to see a mean of the different data types that the dataframe could contain

![df_meteorites.sample(20)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc5ywe0v1ipfg7slho9d.png)
 
Let's see to powerful methods to get information and describe the dataframe 👀

```python
df_meteorites.info()
```
![df_meteorites.info()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvm6divcmnf79qa9cahc.png)
Simple as that we already can see how many NaN values we have for each column by comparing the non null values of the **id** which is  45716 with non null values of year for example which is 45425, with that we already know the amount of NaN values on **Year** column, is the subtraction of 45716 **(total rows that we get by using shape)** and non null values of **Year** 45425 then we have **291 NaN values** on **Year** field.

That was just an example of how to use the data from **info()** method. It's a world of possibilities.

Also we can see the data types for each columns and here some tip to parse each column to the right data type 🧐

```python
df_meteorites.convert_dtypes().dtypes
```
![df_meteorites.convert_dtypes().dtypes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8isuvisjutx9atku305.png)
 

the last method that I'd like to show you is **describe()** before that I'll give you a tip. Format the float data before use **describe()** because this will show us data such as ➡️ Standard Deviation, Mean, Max value from each numeric field. 

To do that, we'll use the following line

```python
pd.options.display.float_format = '{:,.2f}'.format
```
Now we can describe the data

```python
df_meteorites.describe(include='all')
```
![df_meteorites.describe(include='all')](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6z6fthgm6w14ghpsdm7v.png)
 
Once we understand the data, the dataframe is able to be queried, here some examples of the function **query()**

We want to see all the meteorites landed on 2013 that were found

```python
df_meteorites.query('year == 2013 and fall == "Found"')
```
![query output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztlaqlf4bnlmvxp6nrs7.png)
 
If you want to add some other tips or feedback I would be more than grateful, thanks for reading my first post see you around ✌️