Home » excel » python – Get Data Based on User Id using pandas with excel sheet data?

python – Get Data Based on User Id using pandas with excel sheet data?

Posted by: admin May 14, 2020 Leave a comment

Questions:

As im tring to fetch specific data based on u_id for ploting graph and predicting market rate.For Example: I have 1000 data in my excel sheet and the U_ID is 5 i want to fetch U_ID 5th entire data. My Data Format is..

enter image description here

Above Screen shot is my excel format input data and i want to fetch data based on U_ID..if the U_ID is 2 means i want to fetch all data includes P_ID,Products(Spoons,cups),Product Quantity,Total amount,location,date.

i tried but still i did not get my expected output.

import pandas as pd
from pandas import DataFrame as df
from pandas import *
import matplotlib.pyplot as plt
from selenium.webdriver.common import by
excel_file = 'FIRST1.xls'
mov = pd.read_excel(excel_file)
# print(mov)
DataFrame(mov, columns=["SNO",  "U_ID", "P_ID", "SUBPRODUCTS",  "PRODUCT QUANTITY", "TOTAL AMOUNT", "LOCATION", "DATE"])
mov.sort_values(by="U_ID", inplace=True)
# df.sort_values(by='PURCHASE AMOUNT')
print (mov.iloc[1])
getda = mov.iloc[1]
gd = getda[1]
od = getda[4]

It give the data as sorted form..Any one help me..
Thanks In Advance

How to&Answers:

Try this:

import pandas as pd
import matplotlib.pyplot as plt

excel_file = 'FIRST1.xls'
df = pd.read_excel(excel_file)
df.columns = ["SNO",  "U_ID", "P_ID", "SUBPRODUCTS",  "PRODUCT QUANTITY", "TOTAL AMOUNT", "LOCATION", "DATE"]

getda = df[df.U_ID == 2].values # <- .values gives you the values of your data row as a list!

gd = getda[1]
od = getda[4]

If you have more than one row that meets the condition then interate over the results (which is a list of lists):

for row in getda:
    gd = row[1]
    od = row[4]