我试图重命名多个数据帧中的列,并将这些列转换为整数。这是我的代码:
def clean_col(df,col_name):
df.reset_index(inplace=True)
df.rename(columns={df.columns[0]:'Date', df.columns[1]: col_name},inplace=True)
df[col_name]=df[col_name].apply(lambda x: int(x))
我有一个dataframe名称字典和列的新名称:
d = {
all_df: "all",
coal_df: "coal",
liquids_df: "liquids",
coke_df: "coke",
natural_gas_df: "natural_gas",
nuclear_df: "nuclear",
hydro_electricity_df: "hydro",
wind_df: "wind",
utility_solar_df: "utility_solar",
geothermal_df: "geo_thermal",
wood_biomass_df: "biomass_wood",
biomass_other_df: "biomass_other",
other_df: "other",
solar_all_df: "all_solar",
}
for i, (key, value) in enumerate(d.items()):
clean_col(key, value)
这就是我得到的错误:
TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed
任何帮助都将不胜感激
我只是创建了两个不同的列表,然后遍历了数据帧列表和新列名列表
通过使用字典链接新旧列名,您走上了正确的道路。如果在数据帧列表中循环;然后循环浏览新的列名字典,这样就行了
使用全局变量(或局部变量)
输出:
相关问题 更多 >
编程相关推荐