pandas - Python: Iterate over dataframes of different lengths, and compute new value with repeat values -


edit: realized set example incorrectly, corrected version follows:

i have 2 dataframes:

df1 = pd.dataframe({'x values': [11, 12, 13], 'time':[1,2.2,3.5}) df2 = pd.dataframe({'x values': [11, 21, 12, 43], 'time':[1,2.1,2.6,3.1}) 

what need iterate on both of these dataframes, , compute new value, ratio of x values in df1 , df2. difficulty comes in because these dataframes of different lengths.

if wanted compute values in two, know use zip, or map. unfortunately, don't want drop values. instead, need able compare time column between 2 frames determine whether or not copy on value previous time computation of in next time period.

so instance, compute first ratio:

df1["x values"][0]/df2["x values"][0] 

then second check update happens next, in case df2, df1["time"] < df2["time"] and:

df1["x values"][0]/df2["x values"][1] 

for third see df1["time"] > df2["time"], third computation be:

df1["x values"][1]/df2["x values"][1] 

the time both values should used compute ratio same "position" if times in 2 dataframes equal.

and on. i'm confused whether or not possible execute using lambda function, or itertools. i've made attempts, have yielded errors. appreciated.

you can merge 2 dataframes on time , calculate ratios

new_df = df1.merge(df2, on = 'time', how = 'outer') new_df['ratio'] = new_df['x values_x'] / new_df['x values_y'] 

you get

    time    x values_x  x values_y  ratio 0   1       11          11          1.000000 1   2       12          21          0.571429 2   2       12          12          1.000000 3   3       13          43          0.302326 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -