无论我做啥,我都会在打印时遇到语法错误。这是下面的代码,我找不到问题所在
Posted
技术标签:
【中文标题】无论我做啥,我都会在打印时遇到语法错误。这是下面的代码,我找不到问题所在【英文标题】:Whatever I do, I get syntax error on print. Here is the code below, i can't find what is wrong无论我做什么,我都会在打印时遇到语法错误。这是下面的代码,我找不到问题所在 【发布时间】:2019-01-12 22:32:33 【问题描述】:import csv
import numpy as np
import pandas as pd
# QUESTION 5; Only looking at the three most populous counties for each state, what are the three
# most populous states (in order of highest population to lowest population)?
# Use `CENSUS2010POP
census_df = pd.read_csv('census.csv')
census_df.head()
def question7():
return "hllo"
def question8():
c = census_df
c = c[(c['REGION'] == 1) | (c['REGION'] == 2)] # region 1 or 2
c = c.where(c['CTYNAME'].str.startswith('Washington')).dropna() # Washington name
c = c[c['POPESTIMATE2015'] > c['POPESTIMATE2014']] #POP 15 > POP 14
c = c.sort_index(ascending=True)
print c[['STNAME', 'CTYNAME']
print (question7())
【问题讨论】:
如果您使用的是 Python 3,则无论您要打印什么,都需要用括号括起来。它不再是一个声明 What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?的可能重复 花药可能重复:Syntax error on print with Python 3 print(question7()) 那么应该在 Python 3 中工作......这应该是一个不同的问题......? 【参考方案1】:线
print c[['STNAME', 'CTYNAME']
缺少用于打印函数调用的括号。改成,
print(c[['STNAME', 'CTYNAME'])
【讨论】:
【参考方案2】:您可以将以下代码用于三个最流行的县:
def answer_six():
largest = census_df.nlargest(3, ['CENSUS2010POP'])
return largest[['STNAME', 'CTYNAME', 'CENSUS2010POP']]
【讨论】:
以上是关于无论我做啥,我都会在打印时遇到语法错误。这是下面的代码,我找不到问题所在的主要内容,如果未能解决你的问题,请参考以下文章