0

I am trying to select China's nationality tourists, this is my code snippet-

import pandas as pd 

China = ses[(ses["S1Country"] == 1)]
List_China = China[['Case','S1Country']]
List_China

This is what i put before the error 

Here I am trying to selecting certain data - most sources that people used, the code snippet to perform it-

import pandas as pd 

Ranking1 = ses[(ses["Q7Infor1"] == 1)]
List_Ranking1 = Ranking1[['China','Q7Infor1']]
List_Ranking1

Then I wrote this code and it reported back to me 'KeyError: "['China'] not in index'

How do I solve it? Thanks for checking in!

sample of the data:

enter image description here

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41

1 Answers1

1

Assuming that you are trying to filter the column Q7Infor1 by the value China, then you can use df[df['col'] == value].

Thus, your original code:

List_Ranking1 = Ranking1[['China','Q7Infor1']]

Becomes this:

List_Ranking1 = Ranking1[Ranking1['Q7Infor1'] == 'China']

Check this answer on the different ways you can filter a column by a row value.


Updated with OP's dataset

'China' is not a valid value in column Q7Infor1. So assuming that China=1, then we can filter by value 1:

China = 1
List_Ranking1 = Ranking1[Ranking1['Q7Infor1'] == China]

To count number of rows:

print(len(List_Ranking1))
user5305519
  • 3,008
  • 4
  • 26
  • 44