Most wanted pandas functions (Part 01)

Hello again.

Among the data scientists who used python, pandas is the most popular data analysis and manipulation library. It contains various functions and methods to perform data preprocessing and analysis.
Here, I'm trying to cover many functions that we can utilize in many projects.For demonstrations, I will use a famous titanic dataset.

###### First let's install the pandas

```python:pandas_funcs.ipynb
!pip install pandas
```

#### 1. Import CSV file.
We can import a csv file by using read_csv function. There are many options for advance usage. Please refer the documents for advance usage.

```python:pandas_funcs.ipynb
df = pd.read_csv("titanic.csv")
```

#### 2. View first N rows.

```python:pandas_funcs.ipynb
df.head(n=7)
```
![1.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/43793368-4b04-77db-0ea4-cee7b4509480.png)

#### 3. View last N rows.

```python:pandas_funcs.ipynb
df.tail(n=7)
```
![2.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/4784df35-3bd4-fc8f-4d40-b3fa1900c4ea.png)

#### 4. View shape of the DataFrame.
```python:pandas_funcs.ipynb
df.shape

# Output >>
(891, 12)
```

#### 5. Get column names.

```python:pandas_funcs.ipynb
df.columns

# Output >>
Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
       'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],
      dtype='object')
```

#### 6. Drop columns.
We can use the drop function to drop the columns or rows. By passing list of column/rows names to drop we can delete those.

```python:pandas_funcs.ipynb
df.drop(['Name','SibSp','Pclass'], axis=1, inplace=True)

df.columns
# Output >>
Index(['PassengerId', 'Survived', 'Sex', 'Age', 'Parch', 'Ticket', 'Fare',
       'Cabin', 'Embarked'],
      dtype='object')
```
Please note that the axis is set to 1 here, which means the function is for columns. If you need to delete rows please use 0.

#### 7.  Sampling.
 We can get a sample very easily by number of rows or by ratio.

```python:pandas_funcs.ipynb
df.shape
# Output >>
(891, 9)

df_sample_1 = df.sample(n=100)
df_sample_1.shape
# Output >>
(100, 9)

df_sample_2 = df.sample(frac=0.75)
df_sample_2.shape
# Output >>
(668, 9)

```
#### 8. Find missing values.
 It is important to deal with the missing values of the data set. Pandas allow us to check the missing values easily by:

```python:pandas_funcs.ipynb
df.isna().sum()
# Output >>
PassengerId      0
Survived         0
Sex              0
Age            177
Parch            0
Ticket           0
Fare             0
Cabin          687
Embarked         2
dtype: int64
```
#### 9. Fill missing values.
Pandas provide a fillna function to fill missing values. It is common to use mean, avg values for this. Note that using method parameters we can fill missing values based on previous or next values. It is useful when we deal with time series data.
For the example : Here there are 177 values missing from the age column. I will use the average age of others to fill the missing values. 

```python:pandas_funcs.ipynb
avg = df['Age'].mean()
df['Age'].fillna(value=avg, inplace=True)
```

#### 10. Drop missing values.
For some projects, sometimes we have to delete entire rows which contain missing values. In that case we can use the dropna function.
In this data set there are 2 values missing from the “Embarked” column. We can remove relevant rows by;    
       
```python:pandas_funcs.ipynb
print(df.shape)
# Output >>
(891, 9)

df.dropna(subset=['Embarked'], axis=0, how='any', inplace=True)
df.shape
# Output >>
(889, 9)
```
Please note that the axis is set to 1 here, which means the function is for columns. If you need to delete rows please use 0.

#### 11. Conditional Selections.
We need to select rows based on certain conditions.  In example below codes show selections of passengers whose gender is male. And age is more than 30 years old. 

```python:pandas_funcs.ipynb
male_30_plus = df[(df.Sex == 'male') & (df.Age> 30)]
male_30_plus
```
![3.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/f0f2f7ae-5fe8-e487-5784-0588a5de0d12.png)

#### 12. Conditional Selections with isin
If we need to check several values, we can use isin function, it will return a dataframe which only contains specific values from the column.

Suppose that we need to get passengers ages is 12 and 15,

```python:pandas_funcs.ipynb
df[df['Age'].isin([12,15])]
```
![4.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/a69d8eea-21f6-4f21-b7bb-fe368c2f51f9.png)

#### 13. Insert new column.
We can easily add a new column to a dataframe like this. 



```python:pandas_funcs.ipynb
group = np.random.randint(10, size=len(df))
df['new_group_name'] = group
df
```

![5.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/863d1202-18ad-da2f-ba4f-4e1f9102c735.png)

If you need to add columns at a specific position , insert function can be used.

```python:pandas_funcs.ipynb
df.insert(2, 'new_group_name', group)
```
#### 14. Ranking
We can rank the values of columns based on their values. Lets rank the passengers according to the ticket price.


```python:pandas_funcs.ipynb
df['rank'] = df['Fare'].rank(method='first', ascending=False).astype('int')
df
```
![6.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/579376/bd7376cd-1305-3e8f-f52c-70afc627c0b8.png)

#### 15. Groupby 
This is a mostly used function in pandas. It is easy to get an overview of the data and relationships among the variables.

```python:pandas_funcs.ipynb
df.groupby(['Embarked','Sex'])['Survived'].count()
# Output >>
Embarked  Sex   
C         female     73
          male       95
Q         female     36
          male       41
S         female    203
          male      441
Name: Survived, dtype: int64
```
In this example we can get the count of survived people vs, who boarded from 3 ports and gender.

Since this article is becoming long, I will continue from part 2 in later date.

*本記事は @qualitia_cdevの中の一人、@nuwanさんが書いてくれました。
*This article is written by @nuwan a member of @qualitia_cdev.