python - 如何根据另一个 df 两列值设置 df 列值
问题描述
我有两个excel文件。一个带有机器生产数据的每一行都有一堆操作系统传感器数据。
Time S1 S2 S3
2019-01-04 05:00:20 -0,068576396 -0,081597209 0,328993082
2019-01-04 05:00:50 -0,071180522 -0,079861104 0,353298664
2019-01-04 05:01:20 -0,073784709 -0,081597209 0,391493082
...
第二个有两个时间戳数据之间产生的内容:
From To product
2019-01-04 04:00:00 2019-01-09 08:00:00 T2887_001
2019-01-04 08:00:00 2019-01-09 12:15:00 T2887_002
2019-01-04 12:15:00 2019-01-09 14:00:00 T2887_003
...
时间戳之间没有联系。
我需要什么:在第一个 excel 文件中,我需要一个额外的列。它的值必须是基于第二个文件开始和结束值的生产产品编号。
老实说,我对 pandas 很陌生,但我阅读了基础知识并找不到我的答案。
我将excel加载到df并保存回来。在df中,当我检查它时,所有必要的列数据类型都是时间戳,但是当我保存为excel并使用openpyxl时,我在python3中加载其中一个列数据类型是float。我不确定为什么。我尝试的是遍历这两个文件以获取我的数据。
import openpyxl
wb = openpyxl.load_workbook('Szárítás összes januar_P.xlsx')
sheet_1 = wb['Sheet1']
wb_gy = openpyxl.load_workbook('Gyártások teszt_P.xlsx')
sheet_gy = wb['Sheet1']
s_gy = 2
while sheet_gy.cell(row=s_gy,column=1).value != None:
s = 2
while sheet_1.cell(row=s,column=1).value != None:
if sheet_1.cell(row=s,column=2).value > sheet_gy.cell(row=s_gy,column=6).value and sheet_1.cell(row=s,column=2).value < sheet_gy.cell(row=s_gy,column=7).value :
sheet_1.cell(row=s,column=16).value = sheet_gy.cell(row=s_gy,column=9).value
s += 1
s_gy += 1
错误:
Traceback (most recent call last):
File "C:\Users\p_jozsi\Desktop\Python\Dipa\Gyártás azonositok kiosztasa\gyartasok.py", line 15, in <module>
if sheet_1.cell(row=s,column=2).value > sheet_gy.cell(row=s_gy,column=6).value and sheet_1.cell(row=s,column=2).value < sheet_gy.cell(row=s_gy,column=7).value :
TypeError: '>' not supported between instances of 'datetime.datetime' and 'float'
我想要这样的东西:
Time S1 S2 S3 product
2019-01-04 05:00:20 -0,068576396 -0,081597209 0,328993082 T2887_001
2019-01-04 05:00:50 -0,071180522 -0,079861104 0,353298664 T2887_001
2019-01-04 05:01:20 -0,073784709 -0,081597209 0,391493082 T2887_001
...
我非常感谢所有的帮助。
约瑟夫
解决方案
使用IntervalIndex.from_arrays
并分配列的匹配值product
:
s = pd.IntervalIndex.from_arrays(df2['From'], df2['To'], 'left')
#print (s)
df1['product'] = df2.set_index(s).loc[df1['Time'], 'product'].values
print (df1)
Time S1 S2 S3 product
0 2019-01-04 05:00:20 -0,068576396 -0,081597209 0,328993082 T2887_001
1 2019-01-04 05:00:50 -0,071180522 -0,079861104 0,353298664 T2887_001
2 2019-01-04 05:01:20 -0,073784709 -0,081597209 0,3914930823 T2887_001
推荐阅读
- angular - 带有参数的特定路由的用户访问
- python - 通过硒,Python中的xpath检测用户可见元素(仅在视口中)
- c# - 如何让 C# 中的按钮上显示的文本?
- reactjs - 动作类型说明
- mysql-python - 如何在 Python 2.6 中使用 mysql.connector
- swift - 在 SpriteKit swift4 Xcode 中停止一个动作并设置一个定时器来切换玩家
- r - 根据列名和因子将数据从一个数据框收集到另一个数据框
- android - getUserConfirmation 不起作用反应本机
- directory - 我在哪里可以找到星际争霸II 中的地图文件夹?
- time-complexity - 计算 C 中特定代码的时间复杂度