簡體   English   中英

從生成器對象提取數據

[英]Extracting data from generator object

預先感謝python新手,感謝您的幫助。

使用多個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,value和column。

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

需要幫助進行排序,嘗試了幾項('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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM