Python(GoogleSheetsAPI)-通过两个单元格的值搜索特定行,并从该行返回特定单元格
在这个问题中,我使用代码,并通过单个单元格的值返回整行 如何返回两个单元格的值,并从此行返回某些单元格 例如,在附表中,您需要从包含单元格“14.05.2021”(日期列)和“Bob”(员工列)的行中返回“值”和“操作”列的值Python(GoogleSheetsAPI)-通过两个单元格的值搜索特定行,并从该行返回特定单元格,python,google-sheets,Python,Google Sheets,在这个问题中,我使用代码,并通过单个单元格的值返回整行 如何返回两个单元格的值,并从此行返回某些单元格 例如,在附表中,您需要从包含单元格“14.05.2021”(日期列)和“Bob”(员工列)的行中返回“值”和“操作”列的值 链接中的代码使用join将sh.row\u值(r.row)转换为字符串,这意味着您在sh.row\u值(r.row)中有一些列表,您可以使用slice获取值 for item in sheet.findall("14.05.2021"): ro
链接中的代码使用
join
将sh.row\u值(r.row)
转换为字符串,这意味着您在sh.row\u值(r.row)
中有一些列表,您可以使用slice获取值
for item in sheet.findall("14.05.2021"):
row = sheet.row_values(item.row)
if row[1] == 'Bob':
print('values:', row[2])
print('actions:', row[3])
但是我认为它可以更简单地与pandas.dataframe一起使用
import pandas as pd
df = pd.DataFrame(sheet.get_all_records())
rows = df[ (df['date'] == "2021.05.14") & (df['employee'] == 'Bob') ]
print( rows )
print( rows[['values', 'actions']] )
最小工作代码。在我的语言(波兰语)中,我需要日期
“YYYY.mm.dd”
如“2021.05.14”
,但您可能需要“14.05.2021”
如何
行[0]
从第一列获取单元格?您尝试了什么?你的密码在哪里?你收到错误信息了吗?始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图,也不是指向外部门户的链接)进行讨论(不是评论)。还有其他有用的信息。code使用,”。join(sh.row_values(r.row))
将其转换为字符串,这样就意味着在sh.row_values(r.row)
中有一些列表,您可以尝试使用[2]
获取行中的第三个元素-sh.row_values(r.row)[2]
。和[3]
一样获得第四个元素。非常感谢,它成功了!对于我的问题,sheet.findall(“14.05.2021”)中的项目出现了以下代码::row=sheet.row\u值(item.row),如果行[1]='Bob':打印(行[2],行[3])
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
scope = [
"https://spreadsheets.google.com/feeds",
'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
sheet = client.open("example-data").sheet1
for item in sheet.findall("2021.05.14"):
row = sheet.row_values(item.row)
if row[1] == 'Bob':
print('values:', row[2])
print('actions:', row[3])
df = pd.DataFrame(sheet.get_all_records())
data = df[ (df['date'] == "2021.05.14") & (df['employee'] == 'Bob') ]
print('---')
print(data)
print('---')
print(data[['values', 'actions']])
print('---')
for index, row in data.iterrows():
print('>>> index:', index)
print('>>> row:', row)
print('>>> values:', row['values'])
print('>>> actions:', row['actions'])
print('---')