import pandas as pd
print("using loc:")
# create data frame
data = [['Rocky', 10, 'Male'], ['Alexa', 12, 'female'], ['siri', 13, 'female']]
# create data frame object using pandas
df = pd.DataFrame(data, columns=['Name', 'Age', 'gender'])
# Set 'Name' column as index
df.set_index("Name", inplace=True)
result = df.loc["Alexa"]
# print result
print(result)
print("\nusing iloc:")
# create data frame
data = [['Rocky', 10, 'Male'], ['Alexa', 12, 'female'], ['siri', 13, 'female']]
# create data frame object using pandas
df = pd.DataFrame(data, columns=['Name', 'Age', 'gender'])
# take index 2 and shows its corresponding data
result = df.iloc[2]
# print result
print(result)
Comments
Leave a comment