pandas deform表的变形操作

Deform Table

pivot

pivot function depends on the uniqueness condition.

df.pivot(index, columns, values)

index is a column or list, as the index of new dataframe.
columns is a column or list, as the columns.
values is a column or list, as the values.

Simply,
pandas pivot
More, index, columns, values are lists.

pandas mulpivot

pivot_table

pivot_table does not depend on the uniqueness.
It’s aggfunc parameter appoints a aggregate function.\

df.pivot_table(index, columns, values, aggfunc, margins)

margins, bool, indicating whether perform marginal aggregation.
Other parameters are same as pivot's.

melt

Reverse process of pivot.

df.melt(id_vars, value_vars, var_name, value_name)

id_vars, a column or list, as the index of the new dataframe.
melt makes column names invalue_vars as a new column named var_name.
And it makes all origin values as a new column named value_name.

pandas melt

pd.wide_to_long

pd.wide_to_long(df,
                stubnames,
                i,
                j,
                sep,
                suffix)

i as index.
stubnames, sep, suffix, function uses RegEx to match these three parts.
It makes suffix as the values of a new index named j.
And it makes stubnames as columns.
pandas wide_to_long

Deform Index

df.unstack(levels)
df.stack(levels)

unstack transforms indexes in levels into columns.
Contrary to unstack, stack transforms columns into indexes.

Other

pd.crosstab

It is similar as to pivot_table

pd.croostab(index, columns, values, aggfunc, dropna)

dropna is a bool value, default True.
Do not include columns whose entries are all NaN.

explode

It could expand elements in a column, which is one of list, tuple, Series, np.ndarray.

df.explode(column)

get_dummies

It converts category features into indicator variables.

pd.get_dummies(df, dummy_na)

dummy_na is a bool value,
indicating whether regards NaN as a dummy.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值