程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

PyPackage01---Pandas11_explode方法使用

編輯:Python


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.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

Case

import
pandas
as

  • 1.
pd.
__version__

  • 1.
'1.1.5'

  • 1.
id
= [
'a',
'b',
'c']

id2 = [ 4, 6,[ 2, 3, 8]]
id3 = [ 1, 1, 1]
df = pd. DataFrame({ 'id': id, 'id2': id2, 'id3': id3})
  • 1.
  • 2.
  • 3.
  • 4.



id

id2

id3

0

a

4

1

1

b

6

1

2

c

[2, 3, 8]

1

df.
explode(
'id2')

  • 1.



id

id2

id3

0

a

4

1

1

b

6

1

2

c

2

1

2

c

3

1

2

c

8

1

2021-03-25 於南京市江寧區九龍湖



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved