根据Python中的String减去2个数据帧中的数字

问题描述 投票:1回答:1

我是绝对的初学者。这里我有两个数据透视表存储在同一个Excel文件的两个不同表中。

df1:
['1C', '1E', '1F', '1H', '1K', '1M', '1N', '1P', '1Q', '1R', '1S', '1U', '1W', '2C', '2E', '2F', '2H', '2K', '2M', '2P', '2S', '2U', '2W']
df2:
['1CA', '1CB', '1CC', '1CF', '1CJ', '1CS', '1CU', '1EA', '1EB', '1EC', '1EF', '1EJ', '1ES', '1FA', '1FB', '1FC', '1FF', '1FJ', '1FS', '1FT', '1FU', '1HA', '1HB', '1HC', '1HF', '1HJ', '1HS', '1HT', '1HU', '1KA', '1KB', '1KC', '1KF', '1KJ', '1KS', '1KU', '1MA', '1MB', '1MC', '1MF', '1MJ', '1MS', '1MU', '1NA', '1NB', '1NC', '1NF', '1NJ', '1PA', '1PB', '1PC', '1PF', '1PJ', '1PS', '1PT', '1PU', '1QA', '1QB', '1QC', '1QF', '1QJ', '1RA', '1RB', '1RC', '1RF', '1RJ', '1SA', '1SB', '1SC', '1SF', '1SJ', '1SS', '1ST', '1SU', '1UA', '1UB', '1UC', '1UF', '1UJ', '1US', '1UU', '1WA', '1WB', '1WC', '1WF', '1WJ', '1WS', '1WU', '2CA', '2CB', '2CC', '2CJ', '2CS', '2EA', '2EB', '2EJ', '2FA', '2FB', '2FC', '2FJ', '2FU', '2HB', '2HC', '2HF', '2HJ', '2HU', '2KA', '2KB', '2KC', '2KF', '2KJ', '2KU', '2MA', '2MB', '2MC', '2MF', '2MJ', '2MS', '2MT', '2PA', '2PB', '2PC', '2PF', '2PJ', '2PU', '2SA', '2SB', '2SC', '2SF', '2SJ', '2UA', '2UB', '2UJ', '2WB', '2WC', '2WF', '2WJ']

df2是df1的子类别。

每张表都有一个数据透视表:

df1:[1 rows x 23 columns]
                1C   1E    1F    1H    1K ...    2M    2P    2S  2U   2W
total          1057  334  3609  3762  1393 ...   328  1611  1426  87  118

df2:[1 rows x 137 columns]
                1CA  1CB  1CC  1CF  1CJ  1CS ...   2UB  2UJ  2WB  2WC  2WF  2WJ
total            11  381  111   20  527    2 ...    47   34   79    2    1   36

我想在表2中用F减去字符串结尾的值。示例:在表2中:1CF1EF1FF等来自各自的字符串,即1C1E1F等。

我的结果应该像"1C= 1C-1CF= 1037"一样,它应该存储在一个新的表格中(这里:表3)。

我的Python代码:

#importing pandas
import pandas as pd
import numpy as np
from openpyxl import load_workbook
#Assigning the worksheet to file
file="Stratification_worksheet.xlsx"
#Loading the spreadsheet 
data= pd.ExcelFile(file)
#sheetname
print(data.sheet_names)
#loading the sheetname to df1
df=data.parse("Auftrag")
print(df)
# creating tuples
L1=["PMC11","PMP11","PMP21","PMC21","PMP23"]
L2=["PTP33B","PTP31B","PTC31B"]
m1=df["ordercode"].str.startswith(tuple(L1))
m2=df["ordercode"].str.startswith(tuple(L2))
#creating a new column preessurerange and slicing the pressure range from order code
a=df["ordercode"].str.slice(10,12)
b=df["ordercode"].str.slice(11,13)
df["pressurerange"]= np.select([m1,m2],[a,b], default =np.nan)
print(df)
#creating a new coloumn Presssureunit and slicing the preesure unit from ordercode
c=df["ordercode"].str.slice(12,13)
d=df["ordercode"].str.slice(14,15)
df["pressureunit"]= np.select([m1,m2],[c,d], default =np.nan)
print(df)  

#creating a tempcolumn to store pressurerange and pressure unit 
df["pressuresensor"]=df["pressurerange"] + df["pressureunit"]
print(df)

#pivottable 
print(df.pivot_table(values="total",columns="pressurerange",aggfunc={"total":np.sum}))

print(df.pivot_table(values="total",columns="pressuresensor",aggfunc={"total":np.sum}))



#creating new worksheet
df1=df.pivot_table(values="total",columns="pressurerange",aggfunc={"total":np.sum})
df2=df.pivot_table(values="total",columns="pressuresensor",aggfunc={"total":np.sum})


book=load_workbook(file)
writer=pd.ExcelWriter(file,engine="openpyxl")
writer.book = book


df1.to_excel(writer,sheet_name="pressurerangepivot")
df2.to_excel(writer,sheet_name="pressuresensorpivot")
writer.save()
writer.close()

"""now we have classified the ordercode based on the pressurerange and pressureunit  and we have the sum under each category""" 
#check the columns
print(list(df))
print(list(df1))
print(list(df2))

我用过suffix="F" df3=df1.iloc[:,:]-df2.iloc[:,:].endswith(suffix,1,2)但它显示错误:

df3=df1['1C']-df2['1CF']

这给了确切的价值。但是我不知道如何使用简单的代码来处理整个数据帧。

python excel dataframe pivot subtraction
1个回答
0
投票
df2= df2.filter(regex=(".*F$"))  # Leave only 'F' columns in sheet2

df2.columns = [i[:-1] for i in df2.columns]  # Remove 'F' in the end for column-wise subtraction

result = df1 - df2  # Substract values
result[result.isnull()] = sheet1  #leaves when there is no "F"
© www.soinside.com 2019 - 2024. All rights reserved.