SyntaxError:解析时出现意外的EOF(使用.format())

时间:2018-11-26 18:59:09

标签: python dataframe syntax format

我已经在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.

1 个答案:

答案 0 :(得分:0)

您忘记了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]))