简体   繁体   English

Python pandas 打印值,其中 column = X 和 row = Y

[英]Python pandas print value where column = X and row = Y

I am relatively new to working with python and pandas and I'm trying to get the value of a cell in an excel sheet with python.我对使用 python 和 pandas 比较陌生,我正在尝试使用 python 获取 Excel 工作表中单元格的值。 To make matters worse, the excel sheet I'm working with doesn't have proper column names.更糟糕的是,我正在使用的 Excel 工作表没有正确的列名。

Here's what the dataframe looks like:这是数据框的样子:

Sign       Name       2020-09-05 2020-09-06 2020-09-07
JD         John Doe   A          A          B
MP         Max Power  B          B          A

What I want to do is to print the value of the "cell" where the column header is the current date and the sign is "MP".我想要做的是打印“单元格”的值,其中列标题是当前日期,符号是“MP”。

What I've tried so far is this:到目前为止我尝试过的是:

import pandas as pd
from datetime import datetime

time=datetime.now()
relevant_sheet = time.strftime("%B" " %y")
current_day = time.strftime("%Y-%m-%d")

excel_file = pd.ExcelFile('theexcelfile.xlsx')
df = pd.read_excel(excel_file, relevant_sheet, skiprows=[0,1,2,3]) # I don't need these
relevant_value = df.loc[df['Sign'] == "MP", df[current_day]]

This gives me a key error for current_day:这给了我 current_day 的一个关键错误:

KeyError: '2020-09-07'

To fully disclose any possible issue with the real dataframe I'm working with: If I just print the dataframe, I get columns that look like this:要完全披露我正在使用的真实数据框的任何可能问题:如果我只是打印数据框,我会得到如下所示的列:

2020-09-01 00:00:00

Which is why I also tried:这就是为什么我也尝试过:

current_day = time.strftime("%Y-%m-%d 00:00:00")

Of course I also "manually" tried all kinds of date formats, but to no avail.当然,我也“手动”尝试了各种日期格式,但无济于事。 Am I going entirely wrong about this?我完全错了吗? Is this excel screwing with me?这是 excel 搞砸了我吗?

If in columns names are datetimes use Timestamp.floor for remove times (set them to 00:00:00 ):如果列名称是日期Timestamp.floor ,则使用Timestamp.floor作为删除时间(将它们设置为00:00:00 ):

current_day = pd.to_datetime('now').floor('d')
print (current_day)
2020-09-07 00:00:00

relevant_value = df.loc[df['Sign'] == "MP", current_day]

If in columns names are datetimes in strings format use:如果列名称是字符串格式的日期时间,请使用:

relevant_value = df.loc[df['Sign'] == "MP", current_day]

If there are python dates:如果有python日期:

current_day = pd.to_datetime('now').date()
print (current_day)
2020-09-07

relevant_value = df.loc[df['Sign'] == "MP", current_day]

You need to pass column name only instead of df[col_name] .您只需要传递列名而不是df[col_name]

Look .loc[] for detail.查看.loc[]了解详细信息。

df.loc[df['Sign'] == "MP", current_day]

Use df.filter to filter the relevant column.使用df.filter过滤相关列。

Get the relevant column by extracting today's date and converting it to a string.通过提取今天的日期并将其转换为字符串来获取相关列。

Proceed and query Sign for MP继续和查询Sign MP

df.loc[df['Sign']=='MP',(dt.date.today()).strftime('%Y-%m-%d')]

Minor changes to how you are doing things will get you the result.对你做事方式的微小改变会让你得到结果。

Step 1: strip out the 00:00:00 (if you want just the date value)第 1 步:去掉 00:00:00(如果你只想要日期值)

Step 2: your condition had an extra df[]第 2 步:您的条件有一个额外的 df[]

#strip last part of the column names if column starts with 2020 
df.rename(columns=lambda x: x[:10] if x[:4] == '2020' else x, inplace=True)

current_day = datetime.date(datetime.now()).strftime("%Y-%m-%d")

relevant_value = df.loc[df['Sign'] == 'MP', current_day] #does not need df before current_day
print(relevant_value)

since you are already using pandas, you don't need to import datetime.由于您已经在使用 Pandas,因此您无需导入 datetime。 you can just give this to get your date in yyyy-mm-dd format你可以给这个以 yyyy-mm-dd 格式获取你的日期

current_day = pd.to_datetime('now').strftime("%Y-%m-%d")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas / Python = Function 通过将 Y 列与在 X 中具有值的另一行匹配来替换 X 列中的 NaN 值 - Pandas / Python = Function that replaces NaN value in column X by matching Column Y with another row that has a value in X 筛选 pandas dataframe 行,其中具有列 A 和值 X 的特定行具有列 B,其值 Y 大于参数 Z - Filter pandas dataframe rows where a specific row with column A and value X has column B with value Y greater than a parameter Z pandas 添加一个新的列类别,其中值 X 和值 Y 之间的现有列 - pandas add a new column category where existing column between value X and value Y Pandas:如果 A 列中的行包含“x”,则将“y”写入 B 列中的行 - Pandas: if row in column A contains "x", write "y" to row in column B 将3列(x,y,结果)Python Pandas DataFrame转换为以x(唯一)为行,y(唯一)为列的结果值的数据帧 - Transform a 3 columns (x, y, result) Python Pandas DataFrame to a DataFrame of result values with x (unique) as row and y (unique) as column 如何在 pandas dataframe 中找到行,其中 row_x 的 col1 值 == row_y 的 col2 值? - How can I find rows in a pandas dataframe where value of col1 for row_x == value of col2 for row_y? Python Pandas如果B列中的值等于[X,Y,Z],则将A列替换为“ T” - Python Pandas If value in column B = equals [X, Y, Z] replace column A with “T” Pandas - 如果列 y 包含 b,则从列中获取值 x - Pandas - Grab value x from column a if column y contains b 删除列值<0的Pandas DataFrame行 - Delete Pandas DataFrame row where column value is < 0 熊猫数据框:查找值从x变为y的位置 - Pandas dataframe: Find location where value changes from x to y
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM