【问题标题】:Dataframe is not defined when trying to concatenate in loop (Python - Pandas)尝试在循环中连接时未定义数据框(Python - Pandas)
【发布时间】:2020-07-15 06:57:20
【问题描述】:

考虑以下列表(命名为 columns_list):

['total_cases',
 'new_cases',
 'total_deaths',
 'new_deaths',
 'total_cases_per_million',
 'new_cases_per_million',
 'total_deaths_per_million',
 'new_deaths_per_million',
 'total_tests',
 'new_tests',
 'total_tests_per_thousand',
 'new_tests_per_thousand',
 'new_tests_smoothed',
 'new_tests_smoothed_per_thousand',
 'tests_units',
 'stringency_index',
 'population',
 'population_density',
 'median_age',
 'aged_65_older',
 'aged_70_older',
 'gdp_per_capita',
 'extreme_poverty',
 'cvd_death_rate',
 'diabetes_prevalence',
 'female_smokers',
 'male_smokers',
 'handwashing_facilities',
 'hospital_beds_per_thousand',
 'life_expectancy']

这些是两个数据框中的列:美国 (df_us) 和加拿大 (df_canada)。我想通过连接来自 df_us 和 df_canada 的相应列,为列表中的每个项目创建一个数据框。

for i in columns_list:
    
    df_i = pd.concat([df_canada[i],df_us[i]],axis=1)

然而,当我输入时

df_new_deaths

我得到以下输出:name 'df_new_deaths' is not defined

为什么?

【问题讨论】:

  • 使用列表条目作为 id 列并存储一个大 df,或者创建一个数据框字典
  • df_i = ... 不会创建df_new_deaths 而只会创建df_i - 你应该使用字典dfs = dict() 然后dfs[i] = ... 你将拥有dfs["new_deaths"]

标签: python pandas


【解决方案1】:
  • 您实际上并未保存数据帧
  • df_new_deaths 从未定义过
  • 将每列的数据框添加到列表中并通过索引访问它
  • 另外,由于只有一列被连接,你最终会得到一个熊猫系列,而不是数据帧,除非你使用pd.DataFrame
df_list = list()
for i in columns_list:
    
    df_list.append(pd.DataFrame(pd.concat([df_canada[i],df_us[i]],axis=1)))
  • 将数据框添加到字典中,其中列名也是键
df_dict = dict()
for i in columns_list:
    
    df_dict[i] = pd.DataFrame(pd.concat([df_canada[i],df_us[i]],axis=1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多