gpt4 book ai didi

python - Pandas df.corr - 跨多列的一个变量

转载 作者:行者123 更新时间:2023-12-01 08:58:01 26 4
gpt4 key购买 nike

我想创建一个相关性,其中一个变量作为自变量,多个附加变量作为因变量,然后将其转换为热图。

我正在关注这些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

最佳答案

解决了。

需要 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();

关于python - Pandas df.corr - 跨多列的一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52670115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com