Pandas-Basics In Short
Published: 2023-01-09 13:03:53

Pandas is a python library that is used to analyse data. It is a table themed library like spreadsheet in excel unlike numpy which had a matrixlike theme. It allows us to analyse, manipulate and explore huge amount of data.

For the basics, we will discuss a few topics-
* Series
* DataFrame
* Missing data
* Groupby
* Merging,joining & concatinating

To start we need to-
```
import numpy as np
import pandas as pd
```


#Series:
**syn=pd.Series(data,index)**

![series ex](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3sa960k9sk4q0iqzr3z.png)
Here data and index can be edited and fixed according to our need. It can be **list, numpy array or even dictionary.**

![series datatype](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9u2awnnviunb3hask68p.png)
Here's some examples-(look up the variations)

![pd-03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvf2m7qkuc9lwmbhsnmj.png)

![pd-04](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oo44sl7824gc2e1t1sih.png)

![pd-05](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipjzm945xts8mezfcjey.png)
If index not mentioned then by default it is added from 0
![pd-06](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekghjc3026g64h52gmhx.png)

![pd-07](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u9jiiebsvqlphxxlplf.png)
In dictionary the keys are the index and values are data
![pd-08](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qv8zyzkzn010mwjapp14.png)
Series is just an idea but we won't see it most often. Its like a string in a list. It won't show a table rather a tablelike presentation. Now what we will use is dataframe which gives us our expected output.
#DataFrame:
**syn=pd.DataFrame(data,index,columns)**
It is the fundamental topic. So we need to know about some of the usage and applications-
* Selection and indexing
* Conditional selection
* Creating new column
* Removing column-row


##Selection and indexing:
###Selecting a row-column:

![selection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzmhvtxwt7dbed9xfqoe.png)
**Syntax:**
Column selection: **arr[column]**- returns a series
Row selection: **arr.loc[row]**- returns a series
Row selection: **arr.iloc[row number(starts from 0)]**

![column](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utzgtkoicwuhazv4odwq.png)

![row](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sd5xi3ey5hzt10esrnli.png)
###Selecting range:
####By rows-
![select range](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nwpxqqxnacml1iwivv7.png)
####By columns-
![range select col](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zobhnssrz9rnkgwmtcbd.png)
###Selecting data:
By combining previous methods we can get a data from the dataframe

![data selection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b08wd607246iwp1c50zy.png)

###Conditional Selection:
Here we apply condition. If we just apply condition, it gives us boolean result. If we call the dataframe then it gives us values true to the condition and NaN in the false ones.

![cond. select](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yl5bhodut7rsi0kbry6w.png)

![cond. select](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9g9pdzyjcj59m67uxpq.png)

We can even combine conditions by 'and'/'or'. But here in pandas to combine conditions we use **'&'**/**'|'** instead of **'and'/'or'**.
![17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gf1k4wskk51ynvo6x6s.png)
![18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5slxr0giphu2xpvztyz.png)

##Creating columns:
syn: arr[column name]=data of the column
![19](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi561d1j0fm14ww3zd2x.png)

##Removing row-column:
axis=0 -> Row
axis=1 -> Column
`inplace=True` is used to make the change permanent
![20](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jg82pm6j0dmwdhxaeihm.png)
![21](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jnvvvzhf2u2e8qqk2k3.png)
by default axis=0

##Missing value:
###Adding missing value:
use np.nan in data

![23](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/869ygubyvmmol1f5f2pl.png)
###Removing NaN:
By default `.dropna()` removes row with NaN
For column use `.dropna(axis=1)`
![24](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9ap7lhqzx2m6zcuagqa.png)
We can also spare row or columns with certain number of true values 
![25](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgnxs7ybm3s8h5bani8l.png)
![26](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bktgr799c8c1rk7wuvwg.png)

##Filling missing values:
![27](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/781h90zxq8bd5726rape.png)

##Groupby:
We can group common data in a column and work with them
![28](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rn9guvuw8cxxcdabav9.png)
![29](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbtysgeqysljt0tou335.png)
After `.groupby()` all common data gets stored...it doesn't print other then when we work with them. Like-
![30](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dyeqxur34pjmzrg6homa.png)
![31](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9fwmruw1biw48wffmwp.png)
try `.min()`, `.max()`, `.describe()`, `.mean()` etc.

##Concatenating, Merging, Joining:
###Concatenating:
to attach column-wise or row-wise: `pd.concat([],axis= )` by default axis=0
![32](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc1rq5lvc1lyd2gxez4p.png)
![33](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edpc35smp9qiyk04b0wq.png)

###Merging:
to attach regarding common column
![34](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ob0byw0yyu5lavhf4lmq.png)
![35](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v347d886ui8ld8v4py0k.png)

###Joining:
to attach regarding common index
![36](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vctezct65ev7liaaz9n4.png)

![37](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ls36h3u8yimhb5fdy3v.png)

##Summary:
![38](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usef479j6eafmifn06xx.png)

This was the basics of pandas. It is really the fundamental stuff. There are features related to file handling, data analysis, plotting etc.
Let's keep exploring...let's dive together😉