在使用 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).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
以上这篇pandas dataframe 中的explode函数用法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。