[英]Iterating through a dictionary of pandas dataframes to rename columns using the rename function in multiple dataframes
a_dict = {'Bristol': '25005','Plymouth': '25023','Worcester': '25027','Hillsborough' :'33011', 'Rockingham':'33015'}
`` a_dict = {'布里斯托尔':'25005','普利茅斯':'25023','伍斯特':'25027','希尔斯堡':'33011','罗金厄姆':'33015'} n_dict = {'Br ':dataBristol,'Pl':dataPlymouth,'W':dataWorcester,'Hillsborough':'H','Rockingham':'R'}
for key, value in a_dict.items():
county = 'data'+str(key)
county = data[data["fips"] == str(value)]
for key1, value1 in n_dict.items():
county1 = value1
county1.rename(columns={'cases':"case"+str(key1), 'deaths': "deaths"+str(key1), 'population': 'pop'+str(key1)}, inplace = True)
AttributeError: 'str' object has no attribute 'rename'
不建议通过字符串名称创建 DataFrames,最好是创建 DataFrames 的字典(并重命名):
d = ({key: data[data["fips"] == str(value)].rename(columns={'cases':"case"+str(key),
'deaths': "deaths"+str(key),
'population': 'pop'+str(key)})
for key, value in a_dict.items()})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.