Create df after looping through another df and generating stats

Viewed 37

I would like to generate a new df

for c in df.select_dtypes(include=[np.number]):
  sk = skew(df[c])
  kt = kurtosis(df[c])

How can i transform this snippet to a dataframe that looks like this:

col_name skewness kurtosis
col_a 0.1 2
col_b 3 0.5
1 Answers

Why not just use pandas.DataFrame.kurtosis and pandas.DataFrame.skew?

Example:

>>> df = pd.DataFrame({'a': ['a', 'b', 'c', 'd', 'e'], 'b': [1, 2, 2, 3, 0], 'c': [-1, 5, 4.6, 10.3, 11]})
>>> pd.concat([df.skew(), df.kurtosis()], axis=1).reset_index().rename({'index': 'col_name', 0: 'skewness', 1: 'kurtosis'}, axis=1)
  col_name  skewness  kurtosis
0        b -0.404796 -0.177515
1        c -0.487970 -0.620300
Related