在Pandas数据框中爆炸一行到多行 [英] Explode a row to multiple rows in pandas dataframe

查看:87
本文介绍了在Pandas数据框中爆炸一行到多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下标头的数据框:

I have a dataframe with the following header:

id, type1, ..., type10, location1, ..., location10

,我想将其转换如下:

id, type, location 

我设法使用嵌入式for循环来做到这一点,但是它非常慢:

I managed to do this using embedded for loops but it's very slow:

new_format_columns = ['ID', 'type', 'location'] 
new_format_dataframe = pd.DataFrame(columns=new_format_columns)



print(data.head())
new_index = 0 
for index, row in data.iterrows(): 
        ID = row["ID"]

        for i in range(1,11):
                if row["type"+str(i)] == np.nan:
                        continue
                else:
                        new_row = pd.Series([ID, row["type"+str(i)], row["location"+str(i)]])
                        new_format_dataframe.loc[new_index] = new_row.values
                        new_index += 1

是否有使用本地熊猫功能进行改进的建议?

Any suggestions for improvement using native pandas features?

推荐答案

您可以使用lreshape:

types = [col for col in df.columns if col.startswith('type')]
location = [col for col in df.columns if col.startswith('location')]

print(pd.lreshape(df, {'Type':types, 'Location':location}, dropna=False))

示例:

import pandas as pd

df = pd.DataFrame({
'type1': {0: 1, 1: 4}, 
'id': {0: 'a', 1: 'a'}, 
'type10': {0: 1, 1: 8},
'location1': {0: 2, 1: 9},
'location10': {0: 5, 1: 7}})

print (df)
  id  location1  location10  type1  type10
0  a          2           5      1       1
1  a          9           7      4       8

types = [col for col in df.columns if col.startswith('type')]
location = [col for col in df.columns if col.startswith('location')]

print(pd.lreshape(df, {'Type':types, 'Location':location}, dropna=False))
  id  Location  Type
0  a         2     1
1  a         9     4
2  a         5     1
3  a         7     8


另一种解决方案,具有双重 melt :


Another solution with double melt:

print (pd.concat([pd.melt(df, id_vars='id', value_vars=types, value_name='type'),
                  pd.melt(df, value_vars=location, value_name='Location')], axis=1)
         .drop('variable', axis=1))

  id  type  Location
0  a     1         2
1  a     4         9
2  a     1         5
3  a     8         7

lreshape 现在没有文档,但是将来有可能会被删除(与pd一起使用.也是).

可能的解决方案是将所有3个功能合并为一个,也许是melt,但现在尚未实现.也许是在一些新版本的熊猫中.然后我的答案将被更新.

Possible solution is merging all 3 functions to one - maybe melt, but now it is not implementated. Maybe in some new version of pandas. Then my answer will be updated.

这篇关于在Pandas数据框中爆炸一行到多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆