Lets say we have the following:
Jan_22 ={'A':221, 'B':119, 'C':455,'E':677}
Feb_22 ={'A':342, 'B':1223,'C':133,'D':3662,'G':321}
Mar_22 ={'A':252, 'C':53}
list = [Jan_22,Feb_22,Mar_22]
df = pd.DataFrame(list)
df
A B C D E G
0 221 119.0 455 NaN 677.0 NaN
1 342 1223.0 133 3662.0 NaN 321.0
2 252 NaN 53 NaN NaN NaN
This dataframe is made from a list of three dictionaries, each named after a month (Jan_22, etc.). The columns are not named by the month (just 0 is Jan,1 is Feb ,2 is Mar). What I want to do is divide the numbers in each column of the dataframe by its respective days in the month. How do I do this?
CodePudding user response:
You can simply use the divide
function of your pandas dataframe:
number_of_days = [31, 28.25, 31]
df = df.divide(number_of_days, axis=0)
CodePudding user response:
Caveat! You are declaring a variable with the reserved word "list" so this may cause an error.
On the other hand, a possible solution to your problem would be:
Jan_22 ={'A':221, 'B':119, 'C':455,'E':677}
Feb_22 ={'A':342, 'B':1223,'C':133,'D':3662,'G':321}
Mar_22 ={'A':252, 'C':53}
lista = [Jan_22,Feb_22,Mar_22]
df = pd.DataFrame(lista)
for i in df.columns.values:
df[i]= df[i].apply(lambda x: x*2) #You can change the two to the` value you want
print(df)