python - 从生成器对象中提取数据

标签 python pandas generator

使用多个 CSV 文件创建我想要使用多个 pandas .asfreq() 选项进行过滤的数据框,创建生成器对象,排序并列出最重要的结果。

import pandas as pd
import numpy as np 

N = 100
dates = pd.date_range('19971002', periods=N, freq='B')
df=pd.DataFrame(np.random.randn(len(dates),1),index=dates,columns=list('A'))
df1=pd.DataFrame(np.random.randn(len(dates),1),index=dates,columns=list('B'))
pieces = (df, df1)
data = pd.concat((pieces), join='outer', axis = 1)
df['custIndex'] = (df.groupby([df.index.year, df.index.month]).cumcount()+1)   # 'CI' =  custIndex increments by 1 for each occurance since month inception

data.head()

time_sets = ['W-Mon', 'W-Tue']
for time_set in time_sets:
    grouped = data.asfreq(time_set).groupby(df.custIndex).sum()
    print time_set
    print grouped.head()


W-Mon
              A         B
custIndex                    
1          1.827512 -0.487051
3         -0.463776 -0.002071
6          2.074173 -0.232500
8         -0.282901  0.575820
11         0.505265 -3.844740
W-Tue
              A         B
custIndex                    
2          1.347802 -0.738638
4          0.273424  0.218833
7          1.439177  3.671049
9          1.722703 -0.962877
12        -3.415453  1.123824

这就是我遇到麻烦的地方,目标是对值列“A”和“B”进行排序(首先是最高值),并提取具有最高值的 custIndex,并列出 custIndex、值和列。

t = (group.sort_index(by='',ascending=True)for key, group in grouped)

需要有关排序依据的帮助,尝试了 several('CI', 'key') 方法,但没有成功。

t
<generator object <genexpr> at 0x000000000AA9A318>

top = pd.DataFrame()

for line in t:
top = top.append(line)

ValueError: need more than 1 value to unpack

目标看起来像:

custIndex   value     time_set  Column
6           2.074173  W_MON     A
1           1.827512  W-MON     A
9           1.722703  W-TUE     B

最佳答案

为了使生成器表达式正常工作,您需要对其进行如下修改:

t = (group.sort_index(ascending=True) for key, group in grouped.iteritems())

尽管它可能“有效”,但它可能仍然无法达到您的预期。要查看输出,您可以尝试:

for line in t:
    print line

对于建议的解决方案,怎么样:

top_n = 5  # The number of top items returned.
goal = pd.DataFrame([[None] * 4] * top_n,  # 4 = number of columns
                    columns=['custIndex', 'value', 'time_set', 'Column'])
for time_set in time_sets:
    grouped = data.asfreq(time_set).groupby(df.custIndex).sum()
    t = (group for group in grouped.unstack().iteritems())
    for [column, custIndex], val in t:
        if val > min(goal.value):
            # Append item to end of goal DataFrame and then re-sort.
            goal.iloc[-1] = [custIndex, val, time_set, column]
            goal.sort('value', ascending=False, inplace=True)

goal.set_index(['custIndex', 'time_set', 'Column'], inplace=True)

>>> goal
                          value
custIndex time_set Column          
12        W-Tue    B       3.048822
5         W-Fri    A        2.63997
18        W-Wed    B       2.570899
10        W-Wed    B       2.493457
19        W-Thu    B       2.164974

关于python - 从生成器对象中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292785/

相关文章:

python - 在python中将包名和函数名作为变量传递

python - Pandas:列求和的过滤结果

python - 在 Flask 中模拟 pymongo

python - 从列中删除多余的字符或值

python - 如何多次产生元素

python - 如何使用strptime转换微秒部分的7位时间戳字符串?

python - 对于大型数据集,更快地将列中的 -1 和 0 替换为 NaN

python - Pandas 使用日期作为索引加入/合并 2 个数据帧

python - 条件随机数生成器 python numpy

python - 如何在生成器中使用 python 上下文管理器