02_python

Python pandas basic: crosstap, fillna, isna

for dream 2023. 5. 7. 23:41
반응형

1) crosstab
- To make frequency table

import pandas as pd
df = pd.read_csv('iris.csv')

pd.crosstab(df.loc[:10, 'Sepal.Width'], df.loc[:10, 'Petal.Width'])
crosstab example

2) isna()
isna() is the same with isnull()
In reverse, notna() is the same with notnull()
 
3) fillna()
To handle nan value easily, fillna() is used.

Signature: df.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)

There are several methods.
First, using the specific value to specific category.

df = pd.read_csv('iris_missing.csv')
df.head(10)
To check missing value
SL_me = round(df['Sepal_Length'].mean(), 1)
print('SL mean:', SL_me)

v = {'Sepal_Length': SL_me}
df = df.fillna(value=v)
df.head(10)
using fillna with value parameter

Second, just filling with one value to the all nan value

df = df.fillna(-1)
df.head(10)
using fillna with just simple method

#python #pandas #basic #nan #fillna #crosstap #frequnecy table
 
 
 

반응형

'02_python' 카테고리의 다른 글

Python pandas basic: iloc, loc, unique, apply  (0) 2023.05.03