python创建excel、并保存_用python在excel中创建和保存数据新表

我是一个绝对的初学者,在下面的代码中我已经使用了pivot表,现在我想将pivot表存储在不同的表中(例如:“sheet 2”和“sheet 3”)。如何创建新工作表并在其中保存数据。

有没有可能把整个工作都保存在令人兴奋的工作表中(在我的例子中是“分层”)_工作表.xlxs) _并_可_通过_MS_Excel_查看_ ? ?_在#importing pandas

import pandas as pd

import numpy as np

#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="pressuresensor",aggfunc={"total":np.sum}))

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

#check the columns

print(list(df))

print(df.dtypes)

你可能感兴趣的:(python创建excel,并保存)