Pandas df.corr-多个cols中的一个变量

时间:2018-10-05 16:48:28

标签: python pandas correlation

我想创建一个关联,其中一个变量为自变量,多个其他变量为因变量,然后将其转换为热图。

我正在关注这些directions

我的代码:

df_corr_interest = df[df.columns[0:11]].corr()['impact_action_yn'][:-1]
#set fig size
fig, ax = plt.subplots(figsize=(20,15))
#plot matrix
sns.heatmap(df_corr_interest, square=True,annot=True, annot_kws={'size':12},cmap="GnBu")
plt.show();

我遇到以下错误:KeyError: 'impact_action_yn'

数据框:

ExternalReference   interest_el interest_pl interest_ad interest_ol commitment_elected  commitment_policy   commitment_advocacy commitment_organizing   timeline_elected    ... Policy  Organizing  Engagement  Parent  Veteran first_gen_american  first_gen_college   ri_region   LGBTQ   Gender
0   0034000001RHCU0AAP  1   1   1   1   0   0   0   0   0   ... 0   0   1   0   0   0   0   0   0   Woman
1   00340000015yDbOAAU  1   1   1   2   0   0   0   2   0   ... 0   1   2   0   0   0   0   1   0   Man
2   0034000000y3QjMAAU  1   2   2   2   0   2   3   4   0   ... 5   2   3   0   0   0   0   1   0   Man
3   0034000001qcNXRAA2  1   1   1   3   0   0   0   3   0   ... 0   1   6   0   0   0   0   1   0   Woman
4   0034000001DVPedAAH  1   1   1   1   0   0   0   0   0   ... 0   0   1   0   0   0   0   0   0   Woman

有什么想法吗?

已更新完整的变量列表:

ExternalReference                   object
interest_el                          int64
interest_pl                          int64
interest_ad                          int64
interest_ol                          int64
commitment_elected                   int64
commitment_policy                    int64
commitment_advocacy                  int64
commitment_organizing                int64
timeline_elected                     int64
timeline_policy                      int64
timeline_advocacy                    int64
timeline_organizing                  int64
interest_appointed                 float64
interest_vol_organizing            float64
interest_school_organizing         float64
impact_action_yn                   float64
impact_action_public_action        float64
impact_action_testified            float64
impact_action_met_el               float64
impact_action_lobbied              float64
impact_action_bill                 float64
impact_action_other                float64
impact_action_other_text            object
impact_topic_charter_schools       float64
impact_topics_ece                  float64
impact_topics_postsecondary        float64
impact_topics_school_choice        float64
impact_topics_student_achv         float64
impact_topics_district_perf        float64
impact_topics_wraparound           float64
impact_topics_school_discipline    float64
impact_topics_special_pops         float64
impact_topics_teacher_tenure       float64
impact_topics_other                float64
impact_topics_other_text            object
impact_role                         object
impact_level                       float64
impact_level_text                   object
impact_success                     float64
impact_other_comments               object
Advocacy                             int64
Elected                              int64
Policy                               int64
Organizing                           int64
Engagement                           int64
Parent                               int64
Veteran                              int64
first_gen_american                   int64
first_gen_college                    int64
ri_region                            int64
LGBTQ                                int64
Gender                              object
dtype: object

2 个答案:

答案 0 :(得分:0)

通过运行df[df.columns[0:11]].corr(),您将获得前11列中每列之间的成对相关性,但是'impact_action_yn'是第15列,则没有任何相关性输入结果。相反,您可以执行df.corr()并随后删除所有不必要的关联。

在那之后,您将遇到sns.reshape需要矩阵的问题;您可以通过将['impact_action_yn']替换为[['impact_action_yn']]来解决此问题。

答案 1 :(得分:0)

解决了。

需要df.to_frame()

完整代码:

df_corr_impact_action_yn = df[df.columns[0:17]].corr()['impact_action_yn'][:-1]
#set fig size
fig, ax = plt.subplots(figsize=(30,25))
#plot matrix
sns.heatmap(df_corr_impact_action_yn.to_frame(),annot=True, annot_kws={'size':12},cmap="GnBu")
plt.show();