Reputation: 127
I tried to get the index of node_id column with specific value but my code doesn't return anything but return the index of option column. I can not tell the reason why there is the diffs between two columns. Could any one give me tips ?? Thank you so much.
client_data = """node_id,option,before_id
1,A,
5,A,4
3,B,2
4,C,1
8,C,2
6,A,
2,A,1
7,C,6
"""
df = pd.read_csv(io.StringIO(client_data), dtype='string', error_bad_lines=False)
before_ind = df.loc[df['node_id'] == 1].index
print(before_ind)
output of before_ind = df.loc[df['node_id'] == 1].index
Int64Index([], dtype='int64')
If I do before_ind = df.loc[df['option'] == 'C'].index
node_id option before_id
3 4 C 1
4 8 C 2
7 7 C 6
Upvotes: 3
Views: 76
Reputation: 24304
The values of 'node id' is string so use:
before_ind = df.loc[df['node_id'] == '1'].index
you can cross verify by this by using .dtypes
attribute:
print(df.dtypes)
#output:
node_id string
option string
before_id string
dtype: object
OR
typecase 'node_id' to int by using astype()
method:
df['nodr id']=df['node id'].astype(int)
#then use:
before_ind = df.loc[df['node_id'] == 1].index
OR
Don't use dtype parameter in read_csv()
method and let pandas to manupulate dtypes:
df = pd.read_csv(io.StringIO(client_data), error_bad_lines=False)
Upvotes: 2
Reputation: 71560
It's because you do it from a StringIO
, which makes it a string.
Try converting it to a integer
:
df['node id'] = df['node id'].astype(int)
print(df.loc[df['node_id'] == 1].index)
Or keep it as a string:
print(df.loc[df['node_id'] == "1"].index)
Upvotes: 1