I have an excel file which contain data like this:-
Prod
Work
Vaction
Year
2022
2022
2023
2022
2022
2023
2022
Month
10
11
12
10
11
12
10
Name
Business?
Exclusive?
Oct
Nov
Dec
Oct
Nov
Dec
Oct
Robert
Yes
No
100
100
100
150
150
150
1.1
Maria
No
Yes
75
75
50
25
25
25
1
and I want to convert this table into this form:
Name
Business?
Exclusive?
Year
Month
Prod
Work
Vacation
Robert
Yes
No
2022
Oct
100
150
1.1
Maria
No
Yes
2022
Nov
100
150
1
Robert
No
Yes
2023
Dec
100
150
1
Maria
No
Yes
2023
Dec
50
150
1
With the help of python pandas library. I am struggling with this problem from so many days. Please Help!
I have a dataframe which has 3 columns [user_id ,year_month & value] , i want to calculate last 6months average for the year automatically for each individual unique user_id and assign it to new column
user_id value year_month
1 50 2021-01
1 54 2021-02
.. .. ..
1 50 2021-11
1 47 2021-12
2 36 2021-01
2 48.5 2021-05
.. .. ..
2 54 2021-11
2 30.2 2021-12
3 41.4 2021-01
3 48.5 2021-02
3 41.4 2021-05
.. .. ..
3 30.2 2021-12
Total year has 12-24 months
to get jan 2022 value[dec 2021 to july 2021]=[55+32+33+63+54+51]/6
to get feb 2022 value[jan 2022 to aug 2021] =[32+33+37+53+54+51]/6
to get mar 2022 value[feb 2022 to sep 2021] =[45+32+33+63+54+51]/6
to get apr 2022 value[mar 2022 to oct 2021] =[63+54+51+45+32+33]/6
First index, your datetime column
df = df.set_index('year_month')
Then do the following
df.groupby('UserId').rolling('6M').transform('avg')
This is the most correct way but hey here is one more intutitive
df.sort_values('year_month').groupby('UserId').rolling(6).transform('avg') # Returns wanted series
As paul h said
Is there any way to return a matrix to a vector? I don't know the number of elements in the matrix, so let's say,matrix has n elements.
Below, it is an example of how I want to transform the table.
Any help, guidance, suggesting, recommendation will be very appreciated.
raw data.csv:
,January,February,March,April,May,June,July,August,September,October,November,December
2019,1,2,3,4,5,6,7,8,9,10,11,12
2018,13,14,15,16,17,18,19,20,21,22,23,24
2017,25,26,27,28,29,30,31,32,33,34,35,36
the link for csv files
raw=pd.read_csv('raw data.csv')
raw.head()
Unnamed: 0 January February March April May June July August September October November December
0 2019 1 2 3 4 5 6 7 8 9 10 11 12
1 2018 13 14 15 16 17 18 19 20 21 22 23 24
2 2017 25 26 27 28 29 30 31 32 33 34 35 36
final=pd.read_csv('Final.csv')
final.head(20)
Year&Month Value
0 2019 January 1
1 2019 February 2
2 2019 March 3
3 2019 April 4
4 2019 May 5
5 2019 June 6
6 2019 July 7
7 2019 August 8
8 2019 September 9
9 2019 October 10
10 2019 November 11
11 2019 December 12
12 2018 January 13
13 2018 February 14
14 2018 March 15
15 2018 April 16
16 2018 May 17
17 2018 June 18
18 2018 July 19
19 2018 August 20```
You can use pandas stack
df = pd.read_csv(r'raw data.csv')
df.set_index(df.columns[0]).stack().reset_index()
Out:
Unnamed: 0 level_1 0
0 2019 January 1
1 2019 February 2
2 2019 March 3
3 2019 April 4
4 2019 May 5
5 2019 June 6
6 2019 July 7
7 2019 August 8
8 2019 September 9
9 2019 October 10
10 2019 November 11
11 2019 December 12
12 2018 January 13
13 2018 February 14
I have a source table data with the below date, week, month and year information.
CALENDAR_DATE WEEK MONTH YEAR
15-Jun-15 25 2015 / 06 2015
16-Jun-15 25 2015 / 06 2015
17-Jun-15 25 2015 / 06 2015
18-Jun-15 25 2015 / 06 2015
19-Jun-15 25 2015 / 06 2015
20-Jun-15 25 2015 / 06 2015
21-Jun-15 26 2015 / 06 2015
22-Jun-15 26 2015 / 06 2015
23-Jun-15 26 2015 / 06 2015
24-Jun-15 26 2015 / 06 2015
25-Jun-15 26 2015 / 06 2015
26-Jun-15 26 2015 / 06 2015
27-Jun-15 26 2015 / 06 2015
28-Jun-15 27 2015 / 06 2015
29-Jun-15 27 2015 / 06 2015
30-Jun-15 27 2015 / 06 2015
I am building a dependent data validation list where i need to extract all unique week numbers for each month as below
2016 2016 / 01 2016 / 02
2016 / 01 1 5
2016 / 02 2 6
2016 / 03 3 7
2016 / 04 4 8
2016 / 05
2016 / 06
2016 / 07
2016 / 08
2016 / 09
2016 / 10
2016 / 11
2016 / 12
Is there a formula with a combination of index, countif and/or vlookup that would serve the purpose. Any guidance would be super helpful
You can do it with an array formula. If you have the dates and week numbers starting in A2 and B2, and the required month number in D1,E1 etc. the formula is:-
=IFERROR(INDEX($B$2:$B$1000,MATCH(1,(COUNTIF(D$2:D2,$B$2:$B$1000)=0)*(MONTH($A$2:$A$1000)=D$1)*($A$2:$A$1000<>""),0)),"")
entered in D3 using CtrlShiftEnter
Note: there must be another approach to this which doesn't need all the week numbers to be listed - just start at the beginning of each month and step forward by 7 days each time, with a bit of logic for the end of the month.
Starting with the first of the month in G3, I came up with this formula to give the 8th, 15th etc. up to the last week of the month. Then you just need to get the WEEKNUM of each of those dates.
=IF((G3+7)>EOMONTH(G3,0),IF(WEEKNUM(EOMONTH(G3,0))>WEEKNUM(G3),EOMONTH(G3,0),""),G3+7)
I have a Values like
Month Price
Jan 10
Feb 20
Mar 30
............
Dec 50
I have a dropdown for selecting month
If user pickedup the month Feb
then the sum should be displayed as 30
Help me out ! tried a lot with excel function ended up with frustration
Very interesting idea.
Formula in B1 =SUM(INDIRECT("E1:E"&MATCH(A1,D:D,0)))
Hope this will help you.
A B C D E
Feb 30 Month Price
Jan 10
Feb 20
Mar 30
Apr 40
May 50
Jun 60
Jul 70
Aug 80
Sep 90
Oct 100
Nov 110
Dec 120