+ 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?
2 Antworten
+ 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])
0
sure enough, works like a charm. Thank you lotses ❤️