pandas dataframe 中的explode函式用法詳解
阿新 • • 發佈:2020-05-19
在使用 pandas 進行資料分析的過程中,我們常常會遇到將一行資料展開成多行的需求,多麼希望能有一個類似於 hive sql 中的 explode 函式。
這個函式如下:
Code
# !/usr/bin/env python # -*- coding:utf-8 -*- # create on 18/4/13 import pandas as pd def dataframe_explode(dataframe,fieldname): temp_fieldname = fieldname + '_made_tuple_' dataframe[temp_fieldname] = dataframe[fieldname].apply(tuple) list_of_dataframes = [] for values in dataframe[temp_fieldname].unique().tolist(): list_of_dataframes.append(pd.DataFrame({ temp_fieldname: [values] * len(values),fieldname: list(values),})) dataframe = dataframe[list(set(dataframe.columns) - set([fieldname]))].merge(pd.concat(list_of_dataframes),how='left',on=temp_fieldname) del dataframe[temp_fieldname] return dataframe df = pd.DataFrame({'listcol':[[1,2,3],[4,5,6]],"aa": [222,333]}) df = dataframe_explode(df,"listcol")
Description
將 dataframe 按照某一指定列進行展開,使得原來的每一行展開成一行或多行。( 注:該列可迭代, 例如list,tuple,set)
補充知識:Pandas列中的字典/列表拆分為單獨的列
我就廢話不多說了,大家還是直接看程式碼吧
[1] df Station ID Pollutants 8809 {"a": "46","b": "3","c": "12"} 8810 {"a": "36","b": "5","c": "8"} 8811 {"b": "2","c": "7"} 8812 {"c": "11"} 8813 {"a": "82","c": "15"}
Method 1:
step 1: convert the Pollutants column to Pandas dataframe series
df_pol_ps = data_df['Pollutants'].apply(pd.Series) df_pol_ps: a b c 0 46 3 12 1 36 5 8 2 NaN 2 7 3 NaN NaN 11 4 82 NaN 15
step 2: concat columns a,b,c and drop/remove the Pollutants
df_final = pd.concat([df,df_pol_ps],axis = 1).drop('Pollutants',axis = 1) df_final: StationID a b c 0 8809 46 3 12 1 8810 36 5 8 2 8811 NaN 2 7 3 8812 NaN NaN 11 4 8813 82 NaN 15
Method 2:
df_final = pd.concat([df,df['Pollutants'].apply(pd.Series)],axis = 1) df_final: StationID a b c 0 8809 46 3 12 1 8810 36 5 8 2 8811 NaN 2 7 3 8812 NaN NaN 11 4 8813 82 NaN 15
以上這篇pandas dataframe 中的explode函式用法詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。