-->

Modifying Code to work for Month and Week instead

2019-09-08 08:15发布

问题:

I am making a stacked bar plot over a year time span where the x-axis is company names, y-axis is the number of calls, and the stacks are the months.

I want to be able to make this plot run for a time span of a month, where the stacks are days, and a time span of a week, where the stacks are days. I am having trouble doing this since my code is built already around the year time span.

My input original input is a csv file. I am pulling two rows like this:

CompanyName     recvd_dttm
Company1        6/5/2015 18:28:50 PM
Company2        6/5/2015 14:25:43 PM
Company3        9/10/2015 21:45:12 PM
Company4        6/5/2015 14:30:43 PM
Company5        6/5/2015 14:32:33 PM

Then I make a datatable that looks like this

pivot_table.head(3)
Out[12]: 
Month       1   2   3   4   5   6   7   8   9   10  11   12 
CompanyName                                                                     
Customer1   17  30  29  39  15  26  24  12  36  21  18   15  
Customer2   4   11  13  22  35  29  15  18  29  31  17   14
Customer3   11   8  25  24   7  15  20   0  21  12  12   17

and my code is this so far.

First I grab a years worth of data (I would change this to a month or a week for this question)

# filter by countries with at least one medal and sort
df['recvd_dttm'] = pd.to_datetime(df['recvd_dttm'])

#Only retrieve data before now (ignore typos that are future dates)
mask = df['recvd_dttm'] <= datetime.datetime.now()
df = df.loc[mask]
# get first and last datetime for final week of data

range_max = df['recvd_dttm'].max()
range_min = range_max - pd.DateOffset(years=1)

# take slice with final week of data
df = df[(df['recvd_dttm'] >= range_min) & 
               (df['recvd_dttm'] <= range_max)]

Then I create the pivot_table shown above.

###########################################################
#Create Dataframe
###########################################################

df = df.set_index('recvd_dttm')
df.index = pd.to_datetime(df.index, format='%m/%d/%Y %H:%M')

result = df.groupby([lambda idx: idx.month, 'CompanyName']).agg(len).reset_index()
result.columns = ['Month', 'CompanyName', 'NumberCalls']
pivot_table = result.pivot(index='Month', columns='CompanyName', values='NumberCalls').fillna(0)
s = pivot_table.sum().sort(ascending=False,inplace=False)
pivot_table = pivot_table.ix[:,s.index[:30]]
pivot_table = pivot_table.transpose()
pivot_table = pivot_table.reset_index()
pivot_table['CompanyName'] = [str(x) for x in pivot_table['CompanyName']]
Companies = list(pivot_table['CompanyName'])
pivot_table = pivot_table.set_index('CompanyName')
pivot_table.to_csv('pivot_table.csv')

Then I use the pivot table to create an OrderedDict for Plotting

###########################################################
#Create OrderedDict for plotting
###########################################################


months = [pivot_table[(m)].astype(float).values for m in range(1, 13)]
names = ["Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov", "Dec"]
months_dict = OrderedDict(list(zip(names, months)))

###########################################################
#Plot!
###########################################################


palette = brewer["RdYlGn"][8]

hover = HoverTool(
    tooltips = [
        ("Month", "@months"),
        ("Number of Calls", "@NumberCalls"),
        ]
)
output_file("stacked_bar.html")
bar = Bar(months_dict, Companies, title="Number of Calls Each Month", palette = palette, legend = "top_right", width = 1200, height=900, stacked=True)
bar.add_tools(hover)


show(bar)

Does anyone have ideas on how to approach modifying this code so it can work for shorter time spans? I am thinking that it will be modification in the OrderedDict section. Possibly making len(recvd_dttm) to iterate over?

回答1:

For days in a month ('2015-07' say) You could change

result = df.groupby([lambda idx: idx.month, 'CompanyName']).agg(len).reset_index()

to something like

month = '2015-07'
result = df.loc[month].groupby([lambda idx: idx.day, 'CompanyName']).agg(len).reset_index()

And replace 'Month' with 'Day' below. You wouldn't have to bother with the OrderedDict etc. in this case as they are just ints. For a week you could do

start, end = '2015-07-06', '2015-07-12'
result = df.loc[start: end].groupby(
            [lambda idx: idx.dayofweek, 'CompanyName']).agg(len).reset_index()