weighted average on a column of dataframe with provided weighted rate - python
Year Week_Number DC_Zip Asin_code
0 2016 1 12206 NaN
1 2016 1 29306 NaN
2 2016 1 33426 NaN
3 2016 1 37206 NaN
4 2016 1 45216 NaN
5 2016 1 60160 NaN
6 2016 1 76110 NaN
7 2016 1 80215 NaN
8 2016 1 84105 NaN
9 2016 1 85034 NaN
10 2016 1 93711 NaN
11 2016 1 98433 NaN
12 2016 2 12206 21.0
13 2016 2 29306 10.0
14 2016 2 33426 11.0
15 2016 2 37206 1.0
16 2016 2 45216 5.0
17 2016 2 60160 7.0
18 2016 2 76110 12.0
19 2016 2 80215 NaN
20 2016 2 84105 2.0
21 2016 2 85034 1.0
22 2016 2 93711 23.0
23 2016 2 98433 7.0
24 2016 3 12206 95.0
25 2016 3 29306 26.0
26 2016 3 33426 51.0
27 2016 3 37206 18.0
28 2016 3 45216 34.0
29 2016 3 60160 30.0
... ... ... ... ...
2778 2020 29 76110 33.0
2779 2020 29 80215 5.0
2780 2020 29 84105 3.0
2781 2020 29 85034 8.0
2782 2020 29 93711 53.0
2783 2020 29 98433 15.0
2784 2020 30 12206 75.0
2785 2020 30 29306 27.0
2786 2020 30 33426 34.0
2787 2020 30 37206 12.0
2788 2020 30 45216 14.0
2789 2020 30 60160 28.0
2790 2020 30 76110 47.0
2791 2020 30 80215 11.0
2792 2020 30 84105 3.0
2793 2020 30 85034 17.0
2794 2020 30 93711 62.0
2795 2020 30 98433 13.0
2796 2020 31 12206 109.0
2797 2020 31 29306 30.0
2798 2020 31 33426 31.0
2799 2020 31 37206 14.0
2800 2020 31 45216 23.0
2801 2020 31 60160 21.0
2802 2020 31 76110 25.0
2803 2020 31 80215 7.0
2804 2020 31 84105 4.0
2805 2020 31 85034 8.0
2806 2020 31 93711 71.0
2807 2020 31 98433 9.0
2808 rows × 4 columns
This is the sales data I am dealing with. I have to perform a weighted average on Asin_code with weighted rate = [5, 5, 20, 30, 40] on respective years 2016, 2017, 2018, 2019 and 2020. I have to create a function so that it will give me a column containing the weighted average of Asin_code."Nan" values should be dropped. We should also change the weighted rate in the future to view more patterns with the data. Any help would be appreciated.
i am trying the following code:
for i in range(len(df.Asin_code)):
df["Weighted_avg"]=rate[0]*df.Asin_code[i]/df.Asin_code.loc[(df.Year==2016)].sum()
just facing difficulties in consolidating the data for whole 5 years.
It becomes much simpler it you define your weights as a dict instead of a list then a simple use of apply() works
# define weights for year as a dict
wr = {2016:5, 2017:5, 2018:20, 2019:30, 2020:40}
df["Weighted_avg"] = df.apply(lambda r:
# numerator is weight * Asin_code[i]
( r["Asin_code"] * wr[r["Year"]]
/
# denomimator sum(Asin_code for year)
df.Asin_code.loc[(df.Year==r["Year"])].sum() ), axis=1)
output
Idx Year Week_Number DC_Zip Asin_code Weighted_avg
25 2016 3 29306 26.0 0.367232
26 2016 3 33426 51.0 0.720339
27 2016 3 37206 18.0 0.254237
28 2016 3 45216 34.0 0.480226
29 2016 3 60160 30.0 0.423729
2778 2020 29 76110 33.0 1.625616
2779 2020 29 80215 5.0 0.246305
2780 2020 29 84105 3.0 0.147783
2781 2020 29 85034 8.0 0.394089
2782 2020 29 93711 53.0 2.610837
suplementary update
Updated request: weighted_average[at index 1]=rate[for year 2016]*Asin_code[at first index of 2016]+rate[for year 2017]*Asin_code[at first index of 2017]+rate[for year 2018]*Asin_code[at first index of 2018]+rate[for year 2019]*Asin_code[at first index of 2019]+rate[for year 2020]*Asin_code[at first index of 2020]
df.dropna().groupby("Year").agg({"Asin_code":"first"}).reset_index()\
.assign(wa=lambda dfa:
dfa.apply(lambda r: r["Asin_code"]*wr[r['Year']],axis=1))["wa"].sum()
df["Weighted_avg"] = df.apply(lambda r: ( (r["Asin_code"] *wr[r["Year"]]).sum(axis = 0)), axis=1)
Output
12 2016 2 12206 21.0 105.0
13 2016 2 29306 10.0 50.0
14 2016 2 33426 11.0 55.0
15 2016 2 37206 1.0 5.0
16 2016 2 45216 5.0 25.0
17 2016 2 60160 7.0 35.0
18 2016 2 76110 12.0 60.0
19 2016 2 80215 NaN NaN
20 2016 2 84105 2.0 10.0
21 2016 2 85034 1.0 5.0
22 2016 2 93711 23.0 115.0
23 2016 2 98433 7.0 35.0
24 2016 3 12206 95.0 475.0
25 2016 3 29306 26.0 130.0
26 2016 3 33426 51.0 255.0
27 2016 3 37206 18.0 90.0
28 2016 3 45216 34.0 170.0
29 2016 3 60160 30.0 150.0
... ... ... ... ... ...
2778 2020 29 76110 33.0 1320.0
2779 2020 29 80215 5.0 200.0
2780 2020 29 84105 3.0 120.0
2781 2020 29 85034 8.0 320.0
2782 2020 29 93711 53.0 2120.0
2783 2020 29 98433 15.0 600.0
2784 2020 30 12206 75.0 3000.0
2785 2020 30 29306 27.0 1080.0
2786 2020 30 33426 34.0 1360.0
2787 2020 30 37206 12.0 480.0
2788 2020 30 45216 14.0 560.0
2789 2020 30 60160 28.0 1120.0
2790 2020 30 76110 47.0 1880.0
2791 2020 30 80215 11.0 440.0
2792 2020 30 84105 3.0 120.0
2793 2020 30 85034 17.0 680.0
2794 2020 30 93711 62.0 2480.0
2795 2020 30 98433 13.0 520.0
2796 2020 31 12206 109.0 4360.0
2797 2020 31 29306 30.0 1200.0
2798 2020 31 33426 31.0 1240.0
2799 2020 31 37206 14.0 560.0
2800 2020 31 45216 23.0 920.0
2801 2020 31 60160 21.0 840.0
2802 2020 31 76110 25.0 1000.0
2803 2020 31 80215 7.0 280.0
2804 2020 31 84105 4.0 160.0
2805 2020 31 85034 8.0 320.0
2806 2020 31 93711 71.0 2840.0
2807 2020 31 98433 9.0 360.0
Got my solution with this.
Related
Substituting values with conditions
I have a dataframe like this one below Air Station Code Humidity Temperature Latitude Longitude St.1 20 10 10.00 10.00 St.2 4 15 25.00 30.00 St.3 16 21 8.00 15.00 St.4 38 8 31.00 40.00 St.5 10 18 10.00 10.00 St.6 40 4 25.00 30.00 St.7 10 13 8.00 15.00 St.8 46 11 31.00 40.00 St.9 28 9 10.00 10.00 St.10 14 22 25.00 30.00 St.11 5 40 8.00 15.00 St.12 11 10 31.00 40.00 ... St.89 61 35 10.00 10.00 St.90 23 29 25.00 30.00 St.91 35 12 8.00 15.00 St.92 31 7 31.00 40.00 I want to change the station codes by matching the coordinates, substituing the codes by repeating the first 4 codes, obtaining this Air Station Code Humidity Temperature Latitude Longitude St.1 20 10 10.00 10.00 St.2 4 15 25.00 30.00 St.3 16 21 8.00 15.00 St.4 38 8 31.00 40.00 St.1 10 18 10.00 10.00 St.2 40 4 25.00 30.00 St.3 10 13 8.00 15.00 St.4 46 11 31.00 40.00 St.1 28 9 10.00 10.00 St.2 14 22 25.00 30.00 St.3 5 40 8.00 15.00 St.4 11 10 31.00 40.00 ... St.1 61 35 10.00 10.00 St.2 23 29 25.00 30.00 St.3 35 12 8.00 15.00 St.4 31 7 31.00 40.00 Is there some way to implement an "if/else" substitution on the whole dataframe without going manually over every observation in python?
df['Air Station Code'] = 'St.' + pd.Series(df[['Latitude','Longitude']].astype(str).agg(sum, axis=1).factorize()[0] + 1).astype(str) df Out[77]: Air Station Code Humidity Temperature Latitude Longitude 0 St.1 20 10 10.0 10.0 1 St.2 4 15 25.0 30.0 2 St.3 16 21 8.0 15.0 3 St.4 38 8 31.0 40.0 4 St.1 10 18 10.0 10.0 5 St.2 40 4 25.0 30.0 6 St.3 10 13 8.0 15.0 7 St.4 46 11 31.0 40.0 8 St.1 28 9 10.0 10.0 9 St.2 14 22 25.0 30.0 10 St.3 5 40 8.0 15.0 11 St.4 11 10 31.0 40.0
There may be a better way to do this, but this solves the problem. I create a second dataframe without the duplicates, which keeps the first occurrence of each lat/long. I make lat/long the index and drop the other columns. I then do a "join", adding a new column with the matching lat/long. I then overwrite the original station code with the looked up one. import pandas as pd data = [ ["St.1", 20, 10, 10.00, 10.00], ["St.2", 4, 15, 25.00, 30.00], ["St.3", 16, 21, 8.00, 15.00], ["St.4", 38, 8, 31.00, 40.00], ["St.5", 10, 18, 10.00, 10.00], ["St.6", 40, 4, 25.00, 30.00], ["St.7", 10, 13, 8.00, 15.00], ["St.8", 46, 11, 31.00, 40.00], ["St.9", 28, 9, 10.00, 10.00], ["St.10", 14, 22, 25.00, 30.00], ["St.11", 5, 40, 8.00, 15.00], ["St.12", 11, 10, 31.00, 40.00], ["St.89", 61, 35, 10.00, 10.00], ["St.90", 23, 29, 25.00, 30.00], ["St.91", 35, 12, 8.00, 15.00], ["St.92", 31, 7, 31.00, 40.00], ] column = "Air_Station_Code Humidity Temperature Latitude Longitude".split() df = pd.DataFrame(data,columns=column) print(df) df1 = df.drop_duplicates(['Latitude','Longitude']) df1 = df1[['Air_Station_Code','Latitude','Longitude']] df1.set_index(['Latitude','Longitude'], inplace=True) print(df1) df2 = df.join( df1, on=['Latitude','Longitude'], rsuffix='R' ) print(df2) df['Air_Station_Code'] = df2['Air_Station_CodeR'] print(df) Output: Air_Station_Code Humidity Temperature Latitude Longitude 0 St.1 20 10 10.0 10.0 1 St.2 4 15 25.0 30.0 2 St.3 16 21 8.0 15.0 3 St.4 38 8 31.0 40.0 4 St.5 10 18 10.0 10.0 5 St.6 40 4 25.0 30.0 6 St.7 10 13 8.0 15.0 7 St.8 46 11 31.0 40.0 8 St.9 28 9 10.0 10.0 9 St.10 14 22 25.0 30.0 10 St.11 5 40 8.0 15.0 11 St.12 11 10 31.0 40.0 12 St.89 61 35 10.0 10.0 13 St.90 23 29 25.0 30.0 14 St.91 35 12 8.0 15.0 15 St.92 31 7 31.0 40.0 Air_Station_Code Latitude Longitude 10.0 10.0 St.1 25.0 30.0 St.2 8.0 15.0 St.3 31.0 40.0 St.4 Air_Station_Code Humidity ... Longitude Air_Station_CodeR 0 St.1 20 ... 10.0 St.1 1 St.2 4 ... 30.0 St.2 2 St.3 16 ... 15.0 St.3 3 St.4 38 ... 40.0 St.4 4 St.5 10 ... 10.0 St.1 5 St.6 40 ... 30.0 St.2 6 St.7 10 ... 15.0 St.3 7 St.8 46 ... 40.0 St.4 8 St.9 28 ... 10.0 St.1 9 St.10 14 ... 30.0 St.2 10 St.11 5 ... 15.0 St.3 11 St.12 11 ... 40.0 St.4 12 St.89 61 ... 10.0 St.1 13 St.90 23 ... 30.0 St.2 14 St.91 35 ... 15.0 St.3 15 St.92 31 ... 40.0 St.4 [16 rows x 6 columns] Air_Station_Code Humidity Temperature Latitude Longitude 0 St.1 20 10 10.0 10.0 1 St.2 4 15 25.0 30.0 2 St.3 16 21 8.0 15.0 3 St.4 38 8 31.0 40.0 4 St.1 10 18 10.0 10.0 5 St.2 40 4 25.0 30.0 6 St.3 10 13 8.0 15.0 7 St.4 46 11 31.0 40.0 8 St.1 28 9 10.0 10.0 9 St.2 14 22 25.0 30.0 10 St.3 5 40 8.0 15.0 11 St.4 11 10 31.0 40.0 12 St.1 61 35 10.0 10.0 13 St.2 23 29 25.0 30.0 14 St.3 35 12 8.0 15.0 15 St.4 31 7 31.0 40.0
Filling of NaN values with the average of Quantity corresponding to a particular year
Year Week_Number DC_Zip Asin_code 1 2016 1 84105 NaN 2 2016 1 85034 NaN 3 2016 1 93711 NaN 4 2016 1 98433 NaN 5 2016 2 12206 21.0 6 2016 2 29306 10.0 7 2016 2 33426 11.0 8 2016 2 37206 1.0 9 2017 1 12206 266.0 10 2017 1 29306 81.0 11 2017 1 33426 NaN 12 2017 1 37206 NaN 13 2017 1 45216 99.0 14 2017 1 60160 100.0 15 2017 1 76110 76.0 16 2018 1 12206 562.0 17 2018 1 29306 184.0 18 2018 1 33426 NaN 19 2018 1 37206 NaN 20 2018 1 45216 187.0 21 2018 1 60160 192.0 22 2018 1 76110 202.0 23 2019 1 12206 511.0 24 2019 1 29306 NaN 25 2019 1 33426 224.0 26 2019 1 37206 78.0 27 2019 1 45216 160.0 28 2019 1 60160 NaN 29 2019 1 76110 221.0 30 2020 6 93711 NaN 31 2020 6 98433 NaN 32 2020 7 12206 74.0 33 2020 7 29306 22.0 34 2020 7 33426 32.0 35 2020 7 37206 10.0 36 2020 7 45216 34.0 I want to fill the NaN values with the Average of Asin_code for that particular year.I am able to fill the values for 2016 with this code df["Asin_code"]=df.Asin_code.fillna(df.Asin_code.loc[(df.Year==2016)].mean(),axis=0) But unable to do with the whole dataframe..
Use groupby().transform() and fillna: df['Asin_code'] = df['Asin_code'].fillna(df.groupby('Year').Asin_code.transform('mean'))
Missing replacemente by a loop
i have the following dataframe id value year audit 1 21 2007 NaN 1 36 2008 2011 1 7 2009 Nan 2 44 2007 NaN 2 41 2008 Nan 2 15 2009 Nan 3 51 2007 NaN 3 15 2008 2011 3 51 2009 Nan 4 10 2007 NaN 4 12 2008 Nan 4 24 2009 2011 5 30 2007 2011 5 35 2008 Nan 5 122 2009 Nan Basically, I want to create another variable audit2 where all the cells are 2011, if at least one audit is 2011, for each id. I tried to put an if-statement inside a loop, but I cannot get any results I would like to get this new dataframe id value year audit audit2 1 21 2007 NaN 2011 1 36 2008 2011 2011 1 7 2009 Nan 2011 2 44 2007 NaN NaN 2 41 2008 Nan NaN 2 15 2009 Nan NaN 3 51 2007 NaN 2011 3 15 2008 2011 2011 3 51 2009 Nan 2011 4 10 2007 NaN 2011 4 12 2008 Nan 2011 4 24 2009 2011 2011 5 30 2007 2011 2011 5 35 2008 Nan 2011 5 122 2009 Nan 2011 Could you help me please?
df.groupby('id')['audit'].transform(lambda s: s[s.first_valid_index()] if s.first_valid_index() else np.nan) output: >>> df 0 2011.0 1 2011.0 2 2011.0 3 NaN 4 NaN 5 NaN 6 2011.0 7 2011.0 8 2011.0 9 2011.0 10 2011.0 11 2011.0 12 2011.0 13 2011.0 14 2011.0 Name: audit, dtype: float64
pandas cumulative subtraction in a column
I have a dataframe where I need to do a burndown starting from the baseline and subtracting all the values along, essentially I'm looking for an opposite of DataFrame().cumsum(0): In Use Baseline 3705.0 February 2018 0.0 March 2018 2.0 April 2018 15.0 May 2018 30.0 June 2018 14.0 July 2018 797.0 August 2018 1393.0 September 2018 86.0 October 2018 374.0 November 2018 21.0 December 2018 0.0 January 2019 0.0 February 2019 0.0 March 2019 0.0 April 2019 2.0 unknown 971.0 I cannot find a function to do or, or I'm not looking by the right tags / names. How can this be achieved?
Use DataFrameGroupBy.diff by groups created by diff, comapring by lt < and cumulative sum: g = df['Use'].diff().lt(0).cumsum() df['new'] = df['Use'].groupby(g).diff().fillna(df['Use']) print (df) In Use new 0 Baseline 3705.0 3705.0 1 February 2018 0.0 0.0 2 March 2018 2.0 2.0 3 April 2018 15.0 13.0 4 May 2018 30.0 15.0 5 June 2018 14.0 14.0 6 July 2018 797.0 783.0 7 August 2018 1393.0 596.0 8 September 2018 86.0 86.0 9 October 2018 374.0 288.0 10 November 2018 21.0 21.0 11 December 2018 0.0 0.0 12 January 2019 0.0 0.0 13 February 2019 0.0 0.0 14 March 2019 0.0 0.0 15 April 2019 2.0 2.0 16 unknown 971.0 969.0
You can use pd.Series.diff with fillna. Here's a demo: df = pd.DataFrame({'A': np.random.randint(0, 10, 5)}) df['B'] = df['A'].cumsum() df['C'] = df['B'].diff().fillna(df['B']).astype(int) print(df) A B C 0 1 1 1 1 4 5 4 2 4 9 4 3 2 11 2 4 1 12 1
Fill nulls in columns with non-null values from other columns
Given a dataframe with similar columns having null values in between. How to dynamically fill nulls in the columns with non-null values from other columns without explicitly stating the names of other column names e.g. select first column category1 and fill the null rows with values from other columns of same rows? data = {'year': [2010, 2011, 2012, 2013, 2014, 2015, 2016,2017, 2018, 2019], 'category1': [None, 21, None, 10, None, 30, 31,45, 23, 56], 'category2': [10, 21, 20, 10, None, 30, None,45, 23, 56], 'category3': [10, 21, 20, 10, None, 30, 31,45, 23, 56],} df = pd.DataFrame(data) df = df.set_index('year') df category1 category2 category3 year 2010 NaN 10 10 2011 21 21 21 2012 NaN 20 20 2013 10 10 10 2014 NaN NaN NaN 2015 30 30 NaN 2016 31 NaN 31 2017 45 45 45 2018 23 23 23 2019 56 56 56 After filling category1: category1 category2 category3 year 2010 10 10 10 2011 21 21 21 2012 20 20 20 2013 10 10 10 2014 NaN NaN NaN 2015 30 30 NaN 2016 31 NaN 31 2017 45 45 45 2018 23 23 23 2019 56 56 56
IIUC you can do it this way: In [369]: df['category1'] = df['category1'].fillna(df['category2']) In [370]: df Out[370]: category1 category2 category3 year 2010 10.0 10.0 10.0 2011 21.0 21.0 21.0 2012 20.0 20.0 20.0 2013 10.0 10.0 10.0 2014 NaN NaN NaN 2015 30.0 30.0 30.0 2016 31.0 NaN 31.0 2017 45.0 45.0 45.0 2018 23.0 23.0 23.0 2019 56.0 56.0 56.0
You can use first_valid_index with condition if all values are NaN: def f(x): if x.first_valid_index() is None: return None else: return x[x.first_valid_index()] df['a'] = df.apply(f, axis=1) print (df) category1 category2 category3 a year 2010 NaN 10.0 10.0 10.0 2011 21.0 21.0 21.0 21.0 2012 NaN 20.0 20.0 20.0 2013 10.0 10.0 10.0 10.0 2014 NaN NaN NaN NaN 2015 30.0 30.0 30.0 30.0 2016 31.0 NaN 31.0 31.0 2017 45.0 45.0 45.0 45.0 2018 23.0 23.0 23.0 23.0 2019 56.0 56.0 56.0 56.0
Try This: df['category1']= df['category1'].fillna(df.median(axis=1))