How do I perform rolling division with several columns in Pandas? - python
I'm having problems with pd.rolling() method that returns several outputs even though the function returns a single value.
My objective is to:
Calculate the absolute percentage difference between two DataFrames with 3 columns in each df.
Sum all values
I can do this using pd.iterrows(). But working with larger datasets makes this method ineffective.
This is the test data im working with:
#import libraries
import pandas as pd
import numpy as np
#create two dataframes
values = {'column1': [7,2,3,1,3,2,5,3,2,4,6,8,1,3,7,3,7,2,6,3,8],
'column2': [1,5,2,4,1,5,5,3,1,5,3,5,8,1,6,4,2,3,9,1,4],
"column3" : [3,6,3,9,7,1,2,3,7,5,4,1,4,2,9,6,5,1,4,1,3]
}
df1 = pd.DataFrame(values)
df2 = pd.DataFrame([[2,3,4],[3,4,1],[3,6,1]])
print(df1)
print(df2)
column1 column2 column3
0 7 1 3
1 2 5 6
2 3 2 3
3 1 4 9
4 3 1 7
5 2 5 1
6 5 5 2
7 3 3 3
8 2 1 7
9 4 5 5
10 6 3 4
11 8 5 1
12 1 8 4
13 3 1 2
14 7 6 9
15 3 4 6
16 7 2 5
17 2 3 1
18 6 9 4
19 3 1 1
20 8 4 3
0 1 2
0 2 3 4
1 3 4 1
2 3 6 1
This method produces the output I want by using pd.iterrows()
RunningSum = []
for index, rows in df1.iterrows():
if index > 3:
Div = abs((((df2 / df1.iloc[index-3+1:index+1].reset_index(drop="True").values)-1)*100))
Average = Div.sum(axis=0)
SumOfAverages = np.sum(Average)
RunningSum.append(SumOfAverages)
#printing my desired output values
print(RunningSum)
[991.2698412698413,
636.2698412698412,
456.19047619047626,
616.6666666666667,
935.7142857142858,
627.3809523809524,
592.8571428571429,
350.8333333333333,
449.1666666666667,
1290.0,
658.531746031746,
646.031746031746,
597.4603174603175,
478.80952380952385,
383.0952380952381,
980.5555555555555,
612.5]
Finally, below is my attemt to use pd.rolling() so that I dont need to loop through each row.
def SumOfAverageFunction(vals):
Div = abs((((df2.values / vals.reset_index(drop="True").values)-1)*100))
Average = Div.sum()
SumOfAverages = np.sum(Average)
return SumOfAverages
RunningSums = df1.rolling(window=3,axis=0).apply(SumOfAverageFunction)
Here is my problem because printing RunningSums from above outputs several values and is not close to the results I'm getting using iterrows method. How do I solve this?
print(RunningSums)
column1 column2 column3
0 NaN NaN NaN
1 NaN NaN NaN
2 702.380952 780.000000 283.333333
3 533.333333 640.000000 533.333333
4 1200.000000 475.000000 403.174603
5 833.333333 1280.000000 625.396825
6 563.333333 760.000000 1385.714286
7 346.666667 386.666667 1016.666667
8 473.333333 573.333333 447.619048
9 533.333333 1213.333333 327.619048
10 375.000000 746.666667 415.714286
11 408.333333 453.333333 515.000000
12 604.166667 338.333333 1250.000000
13 1366.666667 577.500000 775.000000
14 847.619048 1400.000000 683.333333
15 314.285714 733.333333 455.555556
16 533.333333 441.666667 474.444444
17 347.619048 616.666667 546.666667
18 735.714286 466.666667 1290.000000
19 350.000000 488.888889 875.000000
20 525.000000 1361.111111 1266.666667
It's just the way rolling behaves, it's going to window around all of the columns and I don't know that there is a way around it. One solution is to apply rolling to a single column, and use the indexes from those windows to slice the dataframe inside your function. Still expensive, but probably not as bad as what you're doing.
Also the output of your first method looks wrong. You're actually starting your calculations a few rows too late.
import numpy as np
def SumOfAverageFunction(vals):
return (abs(np.divide(df2.values, df1.loc[vals.index].values)-1)*100).sum()
vals = df1.column1.rolling(3)
vals.apply(SumOfAverageFunction, raw=False)
Related
Pandas Transform
let's say I have the following dataframe: metric value last_1_day last_7_day points 10 3 9 assists 15 2 12 rebounds 12 1 5 I want to transpose this and concatenate thre values within metric with the other columns. So the result would have 1 row and 9 columns. For example: points_value points_last_1_day points_last_7_day assists_value assists_last_1_day assists_last_7_day rebounds_value rebounds_last_1_day rebounds_last_7_day 10 3 9 15 2 12 12 1 5 What is the best way to do this in pandas?
Try with: df = df.set_index('metric').stack().to_frame().T df.columns = ['_'.join(a) for a in df.columns] Output: points_value points_last_1_day points_last_7_day assists_value assists_last_1_day assists_last_7_day rebounds_value rebounds_last_1_day rebounds_last_7_day 0 10 3 9 15 2 12 12 1 5
How can I copy values from one dataframe to other dataframe fastly
I would like to create on my Dataframe (Global_Dataset) a new column (Col_val) based on the other Dataframe (List_Data). I need a faster code because I have a dataset of 2 million samples and List_data contains 50000 samples. Col_Val must contain the value of column Value according to Col_Key List_Data: id Key Value 1 5 0 2 7 1 3 9 2 Global_Dataset: id Col_Key Col_Val 1 9 2 2 5 0 3 9 2 4 7 1 5 7 1 6 5 0 7 9 2 8 7 1 9 9 2 10 5 0 I have tried this code but it needs a long time to be executed. Is there any other faster way for achieving my goal? Col_Val = [] for i in range (len(List_Data)): for j in range (len(Global_Data)): if List_Data.get_value(i, "Key") == Global_Data.get_value(j, 'Col_Key') : Col_Val.append(List_Data.get_value(i, 'Value')) Global_Data['Col_Val'] = Col_Val PS: I have tried loc and iloc instead of get_value but it works very slow
Try this: data_dict = {key : value for key, value in zip(List_Data['Key'], List_Data['Value'])} Global_Data['Col_Val'] = pd.Series([data_dict[key] for key in Global_Data['Col_Key']]) I don't know how long it will takes on your machine with the amount of data you need to handle, but it should be faster of what you are using now. You could also generate the dictionary with data_dict = {row['Key'] : row['Value'] for _, row in list_data.iterrows()} but on my machine is slower than what I proposed above. It works under the assumption that all the keys in Global_Data['Col_Keys'] are present in List_Data['Key'], otherwise you will get a KeyError.
There is no reason to loop through anything, either manually or with iterrows. If I understand your problem, this should be a simple merge operation. df Key Value id 1 5 0 2 7 1 3 9 2 global_df Col_Key id 1 9 2 5 3 9 4 7 5 7 6 5 7 9 8 7 9 9 10 5 global_df.reset_index()\ .merge(df, left_on='Col_Key', right_on='Key')\ .drop('Key', axis=1)\ .set_index('id')\ .sort_index() Col_Key Value id 1 9 2 2 5 0 3 9 2 4 7 1 5 7 1 6 5 0 7 9 2 8 7 1 9 9 2 10 5 0 Note that the essence of this is the global_df.merge(...), but the extra operations are to keep the original indexing and remove unwanted extra columns. I encourage you to try each step individually to see the results.
How to pass a value from one row to the next one in pandas + python and use it to calculate the same following value recursively
This is my desired output: I am trying to calculate the column df[Value] and df[Value_Compensed]. However, to do that, I need to consider the previous value of the row df[Value_Compensed]. In terms of my table: The first row all the values are 0 The following rows: df[Remained] = previous df[Value_compensed]. Then df[Value] = df[Initial_value] + df[Remained]. Then df[Value_Compensed] = df[Value] - df[Compensation] ...And So on... I am struggling to pass the value of Value_Compensed from one row to the next, I tried with the function shift() but as you can see in the following image the values in df[Value_Compensed] are not correct due to it is not a static value and also it also changes after each row it did not work. Any Idea?? Thanks. Manuel.
You can use apply to create your customised operations. I've made a dummy dataset as you didn't provide the initial dataframe. from itertools import zip_longest # dummy data df = pd.DataFrame(np.random.randint(1, 10, (8, 5)), columns=['compensation', 'initial_value', 'remained', 'value', 'value_compensed'],) df.loc[0] = 0,0,0,0,0 >>> print(df) compensation initial_value remained value value_compensed 0 0 0 0 0 0 1 2 9 1 9 7 2 1 4 9 8 3 3 3 4 5 7 6 4 3 2 5 5 6 5 9 1 5 2 4 6 4 5 9 8 2 7 1 6 9 6 8 Use apply (axis=1) to do row-wise iteration, where you use the initial dataframe as an argument, from which you can then get the previous row x.name-1 and do your calculations. Not sure if I fully understood the intended result, but you can adjust the individual calculations of the different columns in the function. def f(x, data): if x.name == 0: return [0,]*data.shape[1] else: x_remained = data.loc[x.name-1]['value_compensed'] x_value = data.loc[x.name-1]['initial_value'] + x_remained x_compensed = x_value - x['compensation'] return [x['compensation'], x['initial_value'], x_remained, \ x_value, x_compensed] adj = df.apply(f, args=(df,), axis=1) adj = pd.DataFrame.from_records(zip_longest(*adj.values), index=df.columns).T >>> print(adj) compensation initial_value remained value value_compensed 0 0 0 0 0 0 1 5 9 0 0 -5 2 5 7 4 13 8 3 7 9 1 8 1 4 6 6 5 14 8 5 4 9 6 12 8 6 2 4 2 11 9 7 9 2 6 10 1
How to modify values which are one row below the values that meet a condition?
Is there an efficient way to change the value of a previous row whenever a conditional is met in a subsequent entry? Specifically I am wondering if there is anyway to adapt pandas.where to modify the entry in a row prior or subsequent to the conditional test. Suppose Data={'Energy':[12,13,14,12,15,16],'Time':[2,3,4,2,5,6]} DF = pd.DataFrame(Data) DF Out[123]: Energy Time 0 12 2 1 13 3 2 14 4 3 12 2 4 15 5 5 16 6 If I wanted to change the value of Energy to 'X' whenever Time <= 2 I could just do something like. DF['ENERGY']=DF['ENERGY'].where(DF['TIME'] >2,'X') or DF.loc[DF['Time']<=2,'Energy']='X' Which would output Energy Time 0 X 2 1 13 3 2 14 4 3 X 2 4 15 5 5 16 6 But what if I want to change the value of 'Energy' in the row after Time <=2 so that the output would actually be. Energy Time 0 12 2 1 X 3 2 14 4 3 12 2 4 X 5 5 16 6 Is there an easy modification for a vectorized approach to this?
Shift the values one row down using Series.shift and then compare: df.loc[df['Time'].shift() <= 2, 'Energy'] = 'X' df Energy Time 0 12 2 1 X 3 2 14 4 3 12 2 4 X 5 5 16 6 Side note: I assume 'X' is actually something else here, but FYI, mixing strings and numeric data leads to object type columns which is a known pandas anti-pattern.
Set value to slice of a Pandas dataframe
I want to sort a subset of a dataframe (say, between indexes i and j) according to some value. I tried df2=df.iloc[i:j].sort_values(by=...) df.iloc[i:j]=df2 No problem with the first line but nothing happens when I run the second one (not even an error). How should I do ? (I tried also the update function but it didn't do either).
I believe need assign to filtered DataFrame with converting to numpy array by values for avoid align indices: df = pd.DataFrame({'A': [1,2,3,4,3,2,1,4,1,2]}) print (df) A 0 1 1 2 2 3 3 4 4 3 5 2 6 1 7 4 8 1 9 2 i = 2 j = 7 df.iloc[i:j] = df.iloc[i:j].sort_values(by='A').values print (df) A 0 1 1 2 2 1 3 2 4 3 5 3 6 4 7 4 8 1 9 2