【问题标题】:SyntaxError: unexpected EOF while parsing (using .format())SyntaxError:解析时出现意外 EOF(使用 .format())
【发布时间】:2019-04-28 11:16:54
【问题描述】:

我在 Python 中多次使用format,但这个我遇到了麻烦。

解决方案应该很简单,但我不明白......

代码如下:

test_list = df.groupby(['gender', 'admitted'])['student_id'].count()

print('The quantity of female students are {}.'.format(test_list[0] + test_list[1])

test_list 的输出为:

gender  admitted
female  False       183
        True         74
male    False       125
        True        118
Name: student_id, dtype: int64

所以,test_list[0] 是 183,test_list[1] 是 74。

打印的预期结果是:

The quantity of female students are 257.

【问题讨论】:

  • 打印语句缺少结束')'
  • 天啊!!!我为这些简单的错误花费了宝贵的时间。非常感谢。我很惭愧,大声笑!

标签: python dataframe syntax format


【解决方案1】:

您忘记了print 语句中的结尾“)”。因此,解析器在预期之前到达了文件的末尾,从而引发了EOFError

你需要做的就是把它改成这样:

test_list = df.groupby(['gender', 'admitted'])['student_id'].count()

print('The quantity of female students are {}.'.format(test_list[0] + test_list[1]))

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多