Pandas Correlation Groupby

前端 未结 3 2053
灰色年华
灰色年华 2020-12-02 19:04

Assuming I have a dataframe similar to the below, how would I get the correlation between 2 specific columns and then group by the \'ID\' column? I believe the Pandas \'cor

3条回答
  •  猫巷女王i
    2020-12-02 19:29

    In the above answer; since ix has been depreciated use iloc instead with some minor other changes:

    df.groupby('ID')[['Val1','Val2']].corr().iloc[0::2][['Val2']] # to get pandas DataFrame
    

    or

    df.groupby('ID')[['Val1','Val2']].corr().iloc[0::2]['Val2'] # to get pandas Series
    

提交回复
热议问题