首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将较长的列名列表替换为数据集中的小名?

如何将较长的列名列表替换为数据集中的小名?
EN

Stack Overflow用户
提问于 2019-12-07 04:48:47
回答 2查看 85关注 0票数 0
  • --我有一个列名的列表,它们太长了,以致于代码混乱。我想用一个更短的列表替换列名列表。

  • ,这里是列的示例:

代码语言:javascript
复制
    non_multi_choice = ["what_is_the_highest_level_of_formal_education_that_you_have_attained_or_plan_to_attain_within_the_next_2_years",
          "select_the_title_most_similar_to_your_current_role_or_most_recent_title_if_retired_selected_choice",
          "in_what_industry_is_your_current_employer_contract_or_your_most_recent_employer_if_retired_selected_choice",
          "how_many_years_of_experience_do_you_have_in_your_current_role",
          "what_is_your_current_yearly_compensation_approximate_usd",
          "does_your_current_employer_incorporate_machine_learning_methods_into_their_business",
          "of_the_choices_that_you_selected_in_the_previous_question_which_ml_library_have_you_used_the_most_selected_choice",
          "approximately_what_percent_of_your_time_at_work_or_school_is_spent_actively_coding"]
代码语言:javascript
复制
    shorter_names = ["highest_level_of_formal_education",
      "job_title",
      "current_industry",
      "years_of_experience",
      "yearly_compensation",
      "does_your_current_employer_incorporate_machine_learning_methods_into_their_business",
      "which_ml_library_have_you_used_the_most_selected_choice",
      "what_percent_of_your_time_at_work_is_spent_actively_coding"]

  • 我想将第一个列表中的每个名称替换为第二个列表中相应的名称。
EN

回答 2

Stack Overflow用户

发布于 2019-12-07 05:11:43

像这样的事怎么样:

代码语言:javascript
复制
for index, item in enumerate(non_multi_choice):
  non_multi_choice[index] = shorter_names[index]
票数 0
EN

Stack Overflow用户

发布于 2019-12-11 11:01:05

如果这些列包含所有列名,则:

代码语言:javascript
复制
df.columns = shorter_names

如果不是

代码语言:javascript
复制
df.rename(columns = {old:new for old, new in zip(non_multi_choice, shorter_names})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59223093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档