+ 1

Python Pandas not showing type

import pandas as pd data = { 'name': ['James', 'Billy', 'Bob', 'Amy', 'Tom', 'Harry'], 'rank': [4, 1, 3, 5, 2, 6] } df = pd.DataFrame(data, index=data['name']) rank = int(input()) for i in df['name']: if str(rank) in str(df.loc[i]): ser = df.squeeze() print(ser['name']) print(ser['name'].loc[i]) Hello, the above task wants me to output just one item of the series, but as soon as i separate the item from the series, the info tags " Name: name type: object " just disappear, how to prevent?

7th Nov 2021, 4:14 PM
Peter Kuik
Peter Kuik - avatar
2 odpowiedzi
+ 3
Avoid iterating through dataframe. Make use of conditional indexing! Here we index by rank and then output the corresponding name: print(df['name'][df['rank'] == rank])
7th Nov 2021, 6:48 PM
Lisa
Lisa - avatar
0
sure enough, works like a charm. Thank you lotses ❤️
8th Nov 2021, 5:42 AM
Peter Kuik
Peter Kuik - avatar