python - Group rows in Pandas DataFrame based on complex condition -


i have basic dataframe, structured this:

             col1 ind1 ind2 0    key1    12      key2    35 1    key3    56      key4    24      key5    65 

...and 1 this:

    cola 0   key1 1   else 2   else 3   key3 

what need mean value of df1, grouped based on whether ind2 in df2 or not. tried without success; message sais "lengths must match compare" -- of course, don't.

df1 = pd.dataframe({'ind1': [0, 0, 1, 1, 1], 'ind2': ['key1', 'key2', 'key3', 'key4', 'key5'], 'col1': [12, 35, 56, 24, 65]}, ) df1.set_index(['ind1', 'ind2'], inplace=true) df2 = pd.dataframe({'cola': ['key1', 'else', 'else', 'key3']})  print (df1.groupby(df1.index.levels[1] in df2.get_values()).mean()) 

thanks in advance hint!

you want check whether element of df1.index.levels[1] in df2.cola (since need value each row). syntax wrote won't that. instead, should try

df1.groupby(df1.index.levels[1].isin(df2.cola)).mean() 

note isin function returns true/false every element, , fact refer directly df2.cola, since column contains values (reffering df2 instead search values in column names of df2).


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -