Dataframe中的Python查询



我试图在我的数据框中获得特定的国家,但当我在查询中添加and条件时,不知怎的会产生错误。

下面是我的代码。

top_country = 15
country_wise_dataframe_temp = country_wise_dataframe.query('Country == "USA" and Country == "Canada"')
fig_confirmed = px.bar(country_wise_dataframe_temp.sort_values('Confirmed').tail(top_country), x = 'Confirmed', y = 'Country',
text = 'Confirmed', orientation = 'h', color_discrete_sequence=[cnf])
fig_deaths = px.bar(country_wise_dataframe.sort_values('Deaths').tail(top_country), x = 'Deaths', y = 'Country',
text = 'Deaths', orientation = 'h', color_discrete_sequence=[dth])
fig_death_x_confirmed = make_subplots(rows = 5, cols = 2, shared_xaxes = False, horizontal_spacing = 0.14, 
vertical_spacing = 0.1, subplot_titles=('Confirmed Cases', 'Deaths Reported'))
fig_death_x_confirmed.add_trace(fig_confirmed['data'][0], row = 1, col = 1)
fig_death_x_confirmed.add_trace(fig_deaths['data'][0], row = 1, col = 2)
fig_death_x_confirmed.update_layout(height = 3000)
country_wise_dataframe_temp.show()

下面是错误信息。

IndexError                                Traceback (most recent call last) <ipython-input-388-e5656e4a8eeb> in <module>
11                                      vertical_spacing = 0.1, subplot_titles=('Confirmed Cases', 'Deaths Reported'))
12 
---> 13 fig_death_x_confirmed.add_trace(fig_confirmed['data'][0], row = 1, col = 1)
14 fig_death_x_confirmed.add_trace(fig_deaths['data'][0], row = 1, col = 2)
15 
IndexError: tuple index out of range

基本上我只想得到美国和加拿大所有国家的数据。

我现在明白了。而不是用and。我应该用or

country_wise_dataframe_temp = country_wise_dataframe.query('Country == "USA" |Country == "Canada"')

最新更新