# Package and Pandas

** Package **
; functions or class already made by someone. It can be used by using 'import' 


** Pandas **
; used to analyze data in Python. Pandas allows us to use data frame structure.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659454951206/-1bKB5BoX.png align="left")
> How to use Pandas?

> ` import pandas as pd ` and  `pd.'a name of a function'`


### Practice1. Make a dataframe ###

```
import pandas as pd

# Create dataframe named df

items = {'code' : [101, 102, 103, 104, 105, 106, 107, 108],
         '과목': ['수학', '영어', '국어', '체육', '미술', '사회', '도덕', '과학'],
         '수강생':[15, 15, 10, 50, 20, 50, 70, 10],
         '선생님': ['김민수','김현정','강수정', '이나리', '도민성', '강수진', '김진성', '오상배']}
df = pd.DataFrame(items)
df
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659455886534/2ts57q8C6.png align="left")

> ` df.head()`

> shows only top 5 rows.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659456007209/OH9hGExkS.png align="left")


> `df.tail()`

> shows only bottom 5 rows.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659456059637/vDLS1DKZ1.png align="left")


> `df.sample(3)`

> sample data randomly.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659456144022/6OFUapY45.png align="left")



### Practice2. Make a dataframe ###
```
items2 = {'code' : [109, 110],
         '과목': ['컴퓨터', '한자'],
         '수강생':[10, 12],
         '선생님': ['이철민', '김영우']}
df2 = pd.DataFrame(items2)
df2
```
> ** Why is it df2? **

> Since df was already created for items1 above, we need a new df.


** to merge two table(df and df2), use concat. **

```
total_df = pd.concat([df, df2])
total_df
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659456438106/28e8o6b7X.png align="left")


** How to save merged table? **
```
total_df.to_csv('data.csv')
```
> if `index=False` is added, 
```
total_df.to_csv('data.csv', index=False)
```
it does not show 'unnamed' as below.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659456916074/VuSNFxo4m.png align="left")


the file can be downloaded from the left section.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659456631647/AaEDlV5nl.png align="left")

> to import the saved csv file,

```
new_df = pd.read_table('data.csv', sep=',')
new_df
```


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659456752890/NyBhgNfHQ.png align="left")

