Answer to Question #145399 in Python for Sriparvathy k

Question #145399
Write a menudriven program to extract data from dataframe.
a)Row Wise b)Row Wise Series Object(iterrows()).
1
Expert's answer
2020-11-20T10:12:28-0500
import pandas as pd


def menu():
    while True:
        print("\n______MENU______")
        print("1. Extract row wise")
        print("2. Row Wise Series Object(iterrows()).")
        print("3. Exit")
        option = int(input("Enter the valid option [1 - 3] : "))
        if option > 3 or option < 1:
            print("Invalid entry. Try again")
            continue
        break
    return option


def main():
    # creating the dataframe
    # initialize list
    data = [['TOM', 10], ['RIDDLE', 15], ['JESSE', 14]]

    # Create the pandas DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age'])
    print("The dataframe is : ")
    print(df)
    while True:
        option = menu()
        if option == 3:
            print("Exiting the program")
            break
        if option == 1:
            # extract row wise
            # iterating directly using the index
            for i in df.index:
                print("Name : ", df['Name'][i], "\tAge : ", df['Age'][i])
        else:
            # extract series object
            for index, row in df.iterrows():
                print(row["Name"], row["Age"])


main()


OUTPUT:


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