Answer to Question #142135 in Python for Dharmarajan

Question #142135
Write a python to create the dataframe and selecting rows/column using loc and iloc function.
1
Expert's answer
2020-11-04T15:42:04-0500
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS