【问题标题】:Get Data Based on User Id using pandas with excel sheet data?使用带有 Excel 工作表数据的 Pandas 获取基于用户 ID 的数据?
【发布时间】:2019-03-19 13:56:42
【问题描述】:

我想根据 u_id 获取特定数据以绘制图表和预测市场汇率。例如:我的 excel 表中有 1000 个数据,U_ID 为 5,我想获取 U_ID 的第 5 个完整数据。我的数据格式是..

上面的屏幕截图是我的 excel 格式输入数据,我想根据 U_ID 获取数据。如果 U_ID 为 2 表示我想获取所有数据,包括 P_ID、产品(勺子、杯子)、产品数量、总量,地点,日期。

我试过了,但还是没有得到预期的输出。

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]

它以排序形式提供数据..任何人帮助我.. 提前致谢

【问题讨论】:

  • 附带说明:import pandas as pd 就足够了。

标签: python excel


【解决方案1】:

试试这个:

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]

如果您有多个满足条件的行,则对结果(即列表列表)进行交互:

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

【讨论】:

  • 谢谢您的尝试..@petezurich 实际上我的实际输出是如果 U_ID 为 2 意味着我应该获取 U_ID 为 2 的所有列。 [3 2.0 2 'Spoons' 10 200 'hosur ' Timestamp('2017-01-03 00:00:00') and 4 2.0 2 'Cups' 1 1500 'chennai' 04/01/2017]
  • @PriyaSRI 不客气。关于您的评论,请参阅我编辑的答案。
猜你喜欢
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多