pandas 批量读取excel_如何在Python中使用Pandas读取多个excel文件

我希望用户从计算机中选择两个文件,按下“计算”按钮后,程序将显示从这些Excel文件中读取的数据的串扰函数结果。我目前无法同时将两个Excel文件读入程序。这是我的密码import numpy as np

import pandas as pd

from tkinter import filedialog as fd

from tkinter import ttk

from tkinter import *

import os.path as op

fileListA = ""

fileListB = ""

def SetFilePath1():

#Getting the file from PC

filepath = fd.askopenfilename(filetypes =(('Excel Files', '.xls'), ('Excel Files','*.xlsx')),parent=root)

if isinstance(filepath, str):

fileListA = filepath.split(" ")

elif isinstance(filepath, tuple):

fileListA = list(filepath)

elif isinstance(filepath, list):

fileListA = filepath

def SetFilePath2():

#Getting the file from PC

filepath = fd.askopenfilename(filetypes =(('Excel Files', '.xls'), ('Excel Files','*.xlsx')),parent=root)

if isinstance(filepath, str):

fileListB = filepath.split(" ")

elif isinstance(filepath, tuple):

fileListB = list(filepath)

elif isinstance(filepath, list):

fileListB = filepath

def GetFilePath1():

#Getting the file from PC

return fileListA

def GetFilePath2():

#Getting the file from PC

return fileListB

def Calculate():

file1 = GetFilePath1()

file2 = GetFilePath2()

print("Inside Calculate")

print(file1)

print("Inside If Loop")

if file1 == "":

print("File 1 Not Found")

elif file2 == "":

print("File 2 Not Found")

else:

print("Inside If Loop")

#Creating DataFrames as df1 & df2 with Header=None because currently no header in the excel files

df1 = pd.read_excel(open(file1,'rb'), header=None)

#Setting the header for the dataframes as title. This will not change anything in the original files

df1.rename(columns={0 : 'title',},inplace=True)

df2 = pd.read_excel(open(file2,'rb'), header=None)

#Setting the header for the dataframes as title. This will not change anything in the original files

df2.rename(columns={0 : 'title',},inplace=True)

#Doing the mathematics line 24 to line 39

df1['Value'] = (df1.title * 32.7) ** 2

df2['Value'] = (df2.title * 32.7) ** 2

df1['Emen'] = df1.Value * df2.Value

#output is just one value of these sum and sqrt functions

df1['TotalD'] = df1['Emen'].sum()

df1['TotalC'] = df1['Value'].sum()

df2['TotalC'] = df2['Value'].sum()

df1['SqrtC'] = df1.TotalC ** 0.5

df2['SqrtC'] = df2.TotalC ** 0.5

df1['MulG'] = df1.SqrtC * df2.SqrtC

df1['DivH'] = df1.TotalD / df1.MulG

df1['SqH'] = df1.DivH * df1.DivH

print("Here is the Cross-Talk: ")

print(df1.SqH[0])

root = Tk()

root.title("Cross-Talk Calculator")

mainframe = ttk.Frame(root, padding="3 3 12 12")

mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

mainframe.columnconfigure(0, weight=1)

mainframe.rowconfigure(0, weight=1)

ttk.Label(mainframe).grid(column=1, row=1, sticky=(W, E))

ttk.Button(mainframe, text="File 1", command=SetFilePath1).grid(column=1, row=1, sticky=E)

ttk.Label(mainframe).grid(column=2, row=1, sticky=(W, E))

ttk.Button(mainframe, text="File 2", command=SetFilePath2).grid(column=3, row=1, sticky=W)

ttk.Label(mainframe, text="The Cross Talk is: ").grid(column=1, row=5, sticky=E)

ttk.Label(mainframe).grid(column=3, row=3, sticky=(W, E))

ttk.Button(mainframe, text="Calculate", command=lambda: Calculate()).grid(column=3, row=4, sticky=W)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

root.bind('')

root.mainloop()

代码的数学部分工作正常。我使用print()来知道我的代码当前在哪里。我在网上找了很多东西,但找不到解决问题的办法。

提前谢谢

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论

打赏作者

Ms 汪

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值