我需要帮助在 Excel 中使用 Python 中的 pandas 进行连接。我需要在 dataFrame 的前两行中添加当前日期和标题,然后我想要新的 dataFrame 和新的列...
我需要帮助在 Excel 中使用 Python 中的 pandas 进行连接。我需要在 dataFrame 的前两行中添加当前日期和标题,然后我想要新的 dataFrame 和新的列来存储我的测量值。
尝试:df = pd.read_excel(file_path)除外 FileNotFoundError:df1_ = {'日期':[datetime.now()。strftime('%Y-%m-%d'),无],'标题':[\'MST \',无]} df1 = pd.DataFrame(data = df1_)df2 = pd.DataFrame(columns = [\'序列号\',\'模式\',\'条件\',\'传感器 1 [%] \',\'传感器 2 [%] \',\'传感器 3 [%] \'])df = pd.concat([df1,df2],ignore_index = True)#df = pd.DataFrame(columns = [\'序列号\',\'模式\',\'条件\',\'传感器 1 [%]\', \'传感器 2 [%]\', \'传感器 3 [%]\'])
# Extract only new measurements for the current serial number
if current_serial_number in self.measurements:
all_data = []
existing_rows = df[df["Serial Number"] == current_serial_number]
measurements = self.measurements[current_serial_number]
for measurement in measurements:
# Check if this specific measurement is already in the file
if not ((existing_rows["Mode"] == measurement["Mode"]) &
(existing_rows["Condition"] == measurement["Condition"]) &
(existing_rows["Sensor 1 [%]"] == measurement["Sensor 1"]) &
(existing_rows["Sensor 2 [%]"] == measurement["Sensor 2"]) &
(existing_rows["Sensor 3 [%]"] == measurement["Sensor 3"])).any():
all_data.append({
"Serial Number": current_serial_number,
"Mode": measurement["Mode"],
"Condition": measurement["Condition"],
"Sensor 1 [%]": measurement["Sensor 1"],
"Sensor 2 [%]": measurement["Sensor 2"],
"Sensor 3 [%]": measurement["Sensor 3"],
})
#if all_data:
# Also add the max voltage row if it's new
max_voltage_row = {
"Serial Number": current_serial_number,
"Mode": "Max Voltages",
"Condition": "Voltage",
"Sensor 1 [%]": measurements[-1]["Max Voltage 1"],
"Sensor 2 [%]": measurements[-1]["Max Voltage 2"],
"Sensor 3 [%]": measurements[-1]["Max Voltage 3"]
}
all_data.append(max_voltage_row)
if self.pwt_flag:
min_curr_row = {
"Serial Number": current_serial_number,
"Mode": "Minimal Current [A]",
"Condition": self.min_value
}
all_data.append(min_curr_row)
max_curr_row = {
"Serial Number": current_serial_number,
"Mode": "Maximal Current [A]",
"Condition": self.max_value
}
all_data.append(max_curr_row)
if all_data:
new_data_df = pd.DataFrame(all_data)
df = pd.concat([df, new_data_df], ignore_index=True)
df.to_excel(file_path, index=False)