通过使用一列获取数据来扩展数据帧

2024-03-28 13:49:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我从数据帧配置开始

import pandas as pd

getData = lambda n: pd.util.testing.makeTimeDataFrame(n)

origDF = pd.DataFrame([{'weight':70, 'name':'GOLD', 'n':3}, {'weight':30, 'name':'SILVER', 'n':4}])

   n    name  weight
0  3    GOLD      70
1  4  SILVER      30

现在我想通过使用n列获取数据,将此配置数据帧扩展为完整的数据帧。我想要的结果是

res = []
for row in origDF.iterrows():
    tmp = getData(row[1]['n'])
    for c,v in row[1].iteritems():
        if c != 'n':
            tmp[c] = v

    res.append(tmp)

res = pd.concat(res)

                   A         B         C         D    name  weight
2000-01-03 -0.084821 -0.345260 -0.789547  0.001570    GOLD      70
2000-01-04 -0.035577 -1.283943 -0.304142 -0.978453    GOLD      70
2000-01-05  0.014727  0.400858 -0.607918  1.769886    GOLD      70
2000-01-03 -0.644647  2.142646  0.617880 -0.178515  SILVER      30
2000-01-04  0.256490 -1.037556 -0.224503  0.148258  SILVER      30
2000-01-05  0.679844  0.976823 -0.403927 -0.459163  SILVER      30
2000-01-06  0.433366  0.429025  0.951633 -0.026547  SILVER      30    

有没有一个很好的程序,让这个直接没有一个循环?你知道吗


Tags: 数据nameinimportpandasforsilverres
1条回答
网友
1楼 · 发布于 2024-03-28 13:49:22

下面是一个解决方案,它在origDF中循环一个:

In [167]: res = getData(origDF.n.sum())

In [168]: res['name'] = 'N/A'

In [169]: res['weight'] = 0

In [170]: res
Out[170]:
                   A         B         C         D name  weight
2000-01-03  1.097798 -1.537407  0.692180 -0.359577  N/A       0
2000-01-04  1.762158  0.568963  0.420136  0.265061  N/A       0
2000-01-05 -0.241067 -0.471753  0.370949  0.533276  N/A       0
2000-01-06  0.099100 -1.757071 -0.680193  0.261295  N/A       0
2000-01-07 -0.818920  0.201746  1.251089  0.834474  N/A       0
2000-01-10  1.551190 -0.329135  0.323669 -0.365978  N/A       0
2000-01-11 -1.941802  0.496720  0.969223 -0.413078  N/A       0

In [171]: i = 0

In [172]: for idx, row in origDF.iterrows():
   .....:         res.ix[i : i + row.n, 'name'] = row['name']
   .....:         res.ix[i : i + row.n, 'weight'] = row.weight
   .....:         i += row.n
   .....:

In [173]: res
Out[173]:
                   A         B         C         D    name  weight
2000-01-03  1.097798 -1.537407  0.692180 -0.359577    GOLD      70
2000-01-04  1.762158  0.568963  0.420136  0.265061    GOLD      70
2000-01-05 -0.241067 -0.471753  0.370949  0.533276    GOLD      70
2000-01-06  0.099100 -1.757071 -0.680193  0.261295  SILVER      30
2000-01-07 -0.818920  0.201746  1.251089  0.834474  SILVER      30
2000-01-10  1.551190 -0.329135  0.323669 -0.365978  SILVER      30
2000-01-11 -1.941802  0.496720  0.969223 -0.413078  SILVER      30

相关问题 更多 >