将pandas dataframe列从列表平展到它们自己的特定列



我有一个pandas数据框架,其中一列包含包含信息的数组列表。它看起来像这样:

id   basket                                       date
c1   [{'product_id': 'P64', 'price': 1146}]       2020-08-11                                     
c2   [{'product_id': 'P44', 'price': 1426},       2020-08-11 
{'product_id': 'P49', 'price': 1108}]                                          
c3   [{'product_id': 'P60', 'price': 39},         2020-08-11 
{'product_id': 'P49', 'price': 1155},                                             
{'product_id': 'P46', 'price': 178}]

我想把篮子列放平,让它看起来像这样:

id   product_id  price     date
c1   P64         1146      2020-08-11                                     
c2   P44         1426      2020-08-11
c2   P49         1108      2020-08-11
c3   P60           39      2020-08-11
c3   P49         1155      2020-08-11
c3   P46          178      2020-08-11

我实在想不明白,请帮助我将不胜感激。

try:

x = [pd.DataFrame(i) for i in df['basket']]
for idx, data in enumerate(x):
data['id']=df.iloc[idx]['id']
data['date']=df.iloc[idx]['date']
df2 = pd.concat(x).reset_index(drop=True)

df2:

product_id  price   id  date
0   P64         1146    c1  2020-08-11
1   P44         1426    c2  2020-08-11 
2   P49         1108    c2  2020-08-11 
3   P60         39      c3  2020-08-11
4   P49         1155    c3  2020-08-11
5   P46         178     c3  2020-08-11

Split (explosion) pandas dataframe string entry to separated rows .

def explode(df, lst_cols, fill_value='', preserve_index=False):
# make sure `lst_cols` is list-alike
if (lst_cols is not None
and len(lst_cols) > 0
and not isinstance(lst_cols, (list, tuple, np.ndarray, pd.Series))):
lst_cols = [lst_cols]
# all columns except `lst_cols`
idx_cols = df.columns.difference(lst_cols)
# calculate lengths of lists
lens = df[lst_cols[0]].str.len()
# preserve original index values    
idx = np.repeat(df.index.values, lens)
# create "exploded" DF
res = (pd.DataFrame({
col:np.repeat(df[col].values, lens)
for col in idx_cols},
index=idx)
.assign(**{col:np.concatenate(df.loc[lens>0, col].values)
for col in lst_cols}))
# append those rows that have empty lists
if (lens == 0).any():
# at least one list in cells is empty
res = (res.append(df.loc[lens==0, idx_cols], sort=False)
.fillna(fill_value))
# revert the original index order
res = res.sort_index()
# reset index if requested
if not preserve_index:        
res = res.reset_index(drop=True)
return res

你会调用

explode(df, ['basket'], fill_value='')

则必须将键和值拆分为单独的列从Pandas专栏中爆炸字典就是这样做的。

您可以使用:

import pandas
from pandas import json_normalize
combined = pandas.concat([json_normalize(df['basket']) for column in df])

内联for循环为列篮中的每个键创建对象列表。然后是熊猫。Concat,将每个列表连接在一个数据框中,并将其返回为组合式。我用它来扁平化MongoDb查询结果。然后,您必须添加其他列。

最新更新