PyPackage01---Pandas11_explode方法使用

Intro

hive中有explode方法,进行单行转多行的操作。pandas也有类似的功能,versionadded:: 0.25.0。直接看个case。

Signature: df1.explode(column: Union[str, Tuple], ignore_index: bool = False) -> 'DataFrame'
Docstring:
Transform each element of a list-like to a row, replicating index values.

.. versionadded:: 0.25.0

Parameters
----------
column : str or tuple
    Column to explode.
ignore_index : bool, default False
    If True, the resulting index will be labeled 0, 1, …, n - 1.

    .. versionadded:: 1.1.0

Returns
-------
DataFrame
    Exploded lists to rows of the subset columns;
    index will be duplicated for these rows.

Raises
------
ValueError :
    if columns of the frame are not unique.

See Also
--------
DataFrame.unstack : Pivot a level of the (necessarily hierarchical)
    index labels.
DataFrame.melt : Unpivot a DataFrame from wide format to long format.
Series.explode : Explode a DataFrame from list-like columns to long format.

Notes
-----
This routine will explode list-likes including lists, tuples,
Series, and np.ndarray. The result dtype of the subset rows will
be object. Scalars will be returned unchanged. Empty list-likes will
result in a np.nan for that row.

Case

import pandas as pd
pd.__version__
'1.1.5'
id = ['a','b','c']
id2 = [4,6,[2,3,8]]
id3 = [1,1,1]
df = pd.DataFrame({'id':id,'id2':id2,'id3':id3})
df
idid2id3
0a41
1b61
2c[2, 3, 8]1
df.explode('id2')
idid2id3
0a41
1b61
2c21
2c31
2c81

                          2021-03-25 于南京市江宁区九龙湖

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值