python - Using one DataFrame as index for another DataFrame in pandas -


still trying figure out how perform operations multiple dataframes form pandas, in python.

i have following 3 dataframes (d1, d2, , d3): enter image description here

for every user in user_id, need use values in columns df2 index of 'weeks' in df3, , multiply them respective values in df1.

e.g.: user 163, column measurements has value 0.0 (from df2). look-up in df3 @ week 0.0 2. final value computed user/column 2(from df1) times 2 = 4.

i need estimate users in user_id , columns (activity, nutrition, etc.)

any ideas?

i have been playing .apply find hard structure problem correctly.

the key, think, put data together. can work separately iterating , going , forth, easier , robust use pandas merge functionality, this:

import pandas pd  data1 = {'user_id':[163], 'measurements':[2.0]} data2 = {'user_id':[163], 'measurements':[0.0]} data3 = {'weeks':[0.0], 'measurements':[2.0]}  df1 = pd.dataframe(data1) df2 = pd.dataframe(data2) df3 = pd.dataframe(data3)  df = df1.merge(df2, on='user_id', how='outer', suffixes=['_df1', '_df2']) df = df.merge(df3, left_on='measurements_df2', right_on='weeks',               how='outer', suffixes=['', '_df3']) df['new_val'] = df['measurements_df1'] * df['measurements']  in [13]: df out[13]:    measurements_df1  user_id  measurements_df2  measurements  weeks  new_val 0               2.0      163               0.0           2.0    0.0      4.0 

in future it's easier if give reproducible example work with, if can include errors tried, in case know mean being hard figure out how structure question properly. suggest book creator of pandas, wes mckinney.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -