【发布时间】: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"]