I wanted to see each unique value with the number of occurrences (or counts of each unique value) in each of the columns of my dataframe, as shown in the picture below. However, I couldn't manage it by groupby or pivot_table functions. I guess there is an easier method -a one line code- that I couldn't devise or find on google (I mean there should be), but I came up with this at the end:
A concise way of seeing the unique values without value counts is here:
Codes here:df_N=pd.DataFrame()
for i in cat_cols:
s1=[i]
s2=pd.DataFrame(s1)
s2=s2.reset_index()
s2=s2.drop('index', axis=1)
s2 = s2.rename(columns={0: 'Column'})
s3=train_data[i].value_counts().reset_index()
s3 = s3.rename(columns={'index': 'Variable', i: 'Count'})
s4=pd.concat([s2,s3],axis=1)
df_N=pd.concat([df_N,s4])
df_N.style.hide_index().format(na_rep='')
for i in cat_cols:
s1=[i]
s2=pd.DataFrame(s1)
s2=s2.reset_index()
s2=s2.drop('index', axis=1)
s2 = s2.rename(columns={0: 'Column'})
s3=train_data[i].value_counts().reset_index()
s3 = s3.rename(columns={'index': 'Variable', i: 'Count'})
s4=pd.concat([s2,s3],axis=1)
df_N=pd.concat([df_N,s4])
df_N.style.hide_index().format(na_rep='')
# group cat and num cols
from sklearn.compose import make_column_selector as selector
num_cols_selector = selector(dtype_exclude=object)
cat_cols_selector = selector(dtype_include=object)
num_cols = num_cols_selector(train_data)
cat_cols = cat_cols_selector(train_data)
from sklearn.compose import make_column_selector as selector
num_cols_selector = selector(dtype_exclude=object)
cat_cols_selector = selector(dtype_include=object)
num_cols = num_cols_selector(train_data)
cat_cols = cat_cols_selector(train_data)
cat_cols=data.select_dtypes(include=['object']).columns for column in cat_cols: print("For column:",column) print(data[column].unique()) print('-'*50)
Hiç yorum yok:
Yorum Gönder