Powerful Pandas! Part-1
Tags: programming, python, pandas, beginners
Published: 2022-12-25 14:36:45

Today we gonna cover Pandas library.
**Pandas** is a python library which usually use for data manipulation and data analysis. Mostly used in **Data Science** and **Machine Learning**. In this notebook we gonna show how powerful pandas library is!

> Let's get started!

Let's call the numpy and pandas library into our workspace. Here, we are using kaggle notebook where these libraries are already installed. 
```py
import numpy as np
import pandas as pd
```
If these libraries aren't installed in you IDE, you have to install them before calling them.


## 1. Pandas Series
Let's create a series with pandas.
```py
a1=['a','b','c']
my_data=[50,70,30]
ar=np.array(my_data)
d={'a':50,'b':70,'c':30}
```

```py
pd.Series(data=my_data, index=a1)
```
Same thing could be done with:
```py
pd.Series(my_data,a1)
```
and also with:
```py
pd.Series(d)
```

**Indexing in series**
```py
series1=pd.Series([1,2,3,4],['A','B','C','D'])
series1
```
```py
series1['C']
```


## 2. Pandas DataFrames
Call the required library for creating data frame in python with pandas.
```py
import numpy as np
import pandas as pd
```
```py
from numpy.random import randn
```
Setting a fixed seed point as we want to draw the same set of random numbers each time we run the code. Otherwise our result would be vary every time we run the code.
```py
np.random.seed(1011)
```
```py
df=pd.DataFrame(randn(5,4),['A','B','C','D','E'],['W','X','Y','Z'])
df
```
Here is our data frame.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8i84a5c178r9sno2a31.png)

If we want to grab the column 'W', output gives a series

```py
df['W']
```
another way to grab a column like sql
```py
df.W
```
If we want to grab multiple column, output gives a dataframe
```py
df[['W','Z']]
```
**Add a column**
Let's add a column to the data frame

```py
df['H']=df['W']+df['Z']
```
**Delete a column**
To delete a column we will use drop function
```py
df.drop('H',axis=1)
```
But if you run again the dataframe new column is still there, so we have to add another argument.

```py
df.drop('H',axis=1,inplace=True)
```
this permanently deletes the column.

**Selecting rows, labelbased index**:

```py
df.loc[['A','B'],['W','Y']]
```
**Conditional selection**
Select rows where W column value is greater than zero along with Y and X column.

```py
df[df['W']>0][['Y','X']]
```

**Multiple selectio**n: Can you explain what result will give the following code?
```py
df[(df['W']>0) & (df['Y']>1)]
```
```py
df[(df['W']>0) | (df['Y']>1)]
```
**Multi-level index or index higher key**
Now we will create a data frame with index more than one level.
```py
outside=['G1','G1','G1','G2','G2','G2']
inside=[1,2,3,1,2,3]
hi_index=list(zip(outside,inside))
hi_index=pd.MultiIndex.from_tuples(hi_index)
```

```py
df=pd.DataFrame(randn(6,2),hi_index,['A','B'])
```
```py
df
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c016qgzt1lne2nnqcv2v.png)

To grab everything under G1

```py
df.loc['G1']
```
Try to explain which value we want to grab with following code:

```py
df.loc['G2'].loc[2]['B']
```

## 3. Read CSV file
CSV files contains plain text and is a well know format that can be read by everyone including Pandas.
```py
df = pd.read_csv('/kaggle/input/pandas/data_set.csv')

print(df.to_string()) 
```



## 4. Correlations
The relationship between each column in your data set can be calculated by cor() method. The relationship between the columns of our data
```py
df.corr()
```
Correlation value varies from -1 to 1. Negative value indicate negative relationship that is if values of variable increases, other will decreases. Positive value mean a positive relationship, values of variable increases, other will increase too. 1 indicates perfect relationship.

You can practice more example at your own. The notebook link is given below. Go to the link and practice.
Notebook Link: [https://www.kaggle.com/code/azizaafrin/powerful-pandas-part-1]

Happy Learning!❤️

_Aziza Afrin_