Reputation: 95
Use several CSV files to create data frame that I want to filter with several pandas .asfreq()
options, create generator object, sort and list top results.
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
Here's where I get in trouble, goal is to sort values column 'A' and 'B' (top values first), and extract custIndex with highest values, and list custIndex, value and column.
t = (group.sort_index(by='',ascending=True)for key, group in grouped)
Need help with what to sort by, tried several('CI', 'key')
things without luck.
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
Goal would look like:
custIndex value time_set Column
6 2.074173 W_MON A
1 1.827512 W-MON A
9 1.722703 W-TUE B
Upvotes: 1
Views: 8236
Reputation: 109636
In order for your generator expression to work, you need to amend it as follows:
t = (group.sort_index(ascending=True) for key, group in grouped.iteritems())
Even though it may 'work', it may still not do what you are intending. To see the output, you can try:
for line in t:
print line
For a proposed solution, how about:
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
Upvotes: 2