Python 语法错误 - 按照给定的示例进行操作,无法找出我出错的地方
Python Syntax Error - following the example given and cannot work out where I have gone wrong
我目前正在学习使用 python 3.
我正在按照 instructions/walkthroughs 提供的内容进行一些练习。
我在尝试更改列名时开始出现以下错误:
File "<ipython-input-25-591a166ee92a>", line 5
'human development index (male)':"hdi_m"
^
SyntaxError: invalid syntax
这是我用过的代码:
gender_development.rename(columns={'gdi rank':"gdi_rank",
'gender development index (gdi)':"gdi"
'human development index (male)':"hdi_m"
'life expectancy at birth (female)':"life_expectancy_f"
'life expectancy at birth (male)':"life_expectancy_m"
'expected years of educaton (female)':"expected_education_years_f"
'expected years of education (male)':"expected_education_years_m"
'mean years of education (female)':"mean_education_years_f"
'mean years of education (male)':"mean_education_years_m"
'estimated gross national income per capita (female)':"gross_income_pc_f"
'estimated gross national income per capita (male)':"gross_income_pc_m"
'human development index (female)':"hdi_f"},
inplace=True)
有什么想法吗?
您似乎漏掉了几个逗号,在 python 词典中这些逗号分隔键值对。注意以下代码中多出的逗号:
gender_development.rename(columns={'gdi rank': "gdi_rank",
'gender development index (gdi)': "gdi",
'human development index (male)': "hdi_m",
'life expectancy at birth (female)': "life_expectancy_f",
'life expectancy at birth (male)': "life_expectancy_m",
'expected years of educaton (female)':"expected_education_years_f",
'expected years of education (male)': "expected_education_years_m",
'mean years of education (female)':"mean_education_years_f",
'mean years of education (male)': "mean_education_years_m",
'estimated gross national income per capita (female)':"gross_income_pc_f",
'estimated gross national income per capita (male)': "gross_income_pc_m",
'human development index (female)':"hdi_f"}, inplace = True)
所有编程语言对此类语法错误都非常敏感。尽量注意使用正确的结构 - 随着时间的推移,这对你来说会变得自然。
另外,试试 linter。 linter 会(可能)抱怨,行长超过 80 个字符,缩进不足,视觉上缺少缩进,: 之后缺少 space,等等
需要清除所有错误(缺失的 gender_development 除外):
gender_development.rename(columns={
'gdi rank': "gdi_rank",
'gender development index (gdi)': "gdi",
'human development index (male)': "hdi_m",
'life expectancy at birth (female)': "life_expectancy_f",
'life expectancy at birth (male)': "life_expectancy_m",
'expected years of educaton (female)': "expected_education_years_f",
'expected years of education (male)': "expected_education_years_m",
'mean years of education (female)': "mean_education_years_f",
'mean years of education (male)': "mean_education_years_m",
'estimated gross national income per capita (female)': "gross_income_pc_f",
'estimated gross national income per capita (male)': "gross_income_pc_m",
'human development index (female)': "hdi_f"
}, inplace=True)
我目前正在学习使用 python 3.
我正在按照 instructions/walkthroughs 提供的内容进行一些练习。
我在尝试更改列名时开始出现以下错误:
File "<ipython-input-25-591a166ee92a>", line 5
'human development index (male)':"hdi_m"
^
SyntaxError: invalid syntax
这是我用过的代码:
gender_development.rename(columns={'gdi rank':"gdi_rank",
'gender development index (gdi)':"gdi"
'human development index (male)':"hdi_m"
'life expectancy at birth (female)':"life_expectancy_f"
'life expectancy at birth (male)':"life_expectancy_m"
'expected years of educaton (female)':"expected_education_years_f"
'expected years of education (male)':"expected_education_years_m"
'mean years of education (female)':"mean_education_years_f"
'mean years of education (male)':"mean_education_years_m"
'estimated gross national income per capita (female)':"gross_income_pc_f"
'estimated gross national income per capita (male)':"gross_income_pc_m"
'human development index (female)':"hdi_f"},
inplace=True)
有什么想法吗?
您似乎漏掉了几个逗号,在 python 词典中这些逗号分隔键值对。注意以下代码中多出的逗号:
gender_development.rename(columns={'gdi rank': "gdi_rank",
'gender development index (gdi)': "gdi",
'human development index (male)': "hdi_m",
'life expectancy at birth (female)': "life_expectancy_f",
'life expectancy at birth (male)': "life_expectancy_m",
'expected years of educaton (female)':"expected_education_years_f",
'expected years of education (male)': "expected_education_years_m",
'mean years of education (female)':"mean_education_years_f",
'mean years of education (male)': "mean_education_years_m",
'estimated gross national income per capita (female)':"gross_income_pc_f",
'estimated gross national income per capita (male)': "gross_income_pc_m",
'human development index (female)':"hdi_f"}, inplace = True)
所有编程语言对此类语法错误都非常敏感。尽量注意使用正确的结构 - 随着时间的推移,这对你来说会变得自然。
另外,试试 linter。 linter 会(可能)抱怨,行长超过 80 个字符,缩进不足,视觉上缺少缩进,: 之后缺少 space,等等
需要清除所有错误(缺失的 gender_development 除外):
gender_development.rename(columns={
'gdi rank': "gdi_rank",
'gender development index (gdi)': "gdi",
'human development index (male)': "hdi_m",
'life expectancy at birth (female)': "life_expectancy_f",
'life expectancy at birth (male)': "life_expectancy_m",
'expected years of educaton (female)': "expected_education_years_f",
'expected years of education (male)': "expected_education_years_m",
'mean years of education (female)': "mean_education_years_f",
'mean years of education (male)': "mean_education_years_m",
'estimated gross national income per capita (female)': "gross_income_pc_f",
'estimated gross national income per capita (male)': "gross_income_pc_m",
'human development index (female)': "hdi_f"
}, inplace=True)