通过要求用户从目录中选择Excel工作簿和工作表,将Excel文件转换为PANDAS数据框架
我希望通过要求用户从其目录中选择文件来创建大熊猫的数据框架。不过,除了选择文件外,我还希望在文件中指定特定表格。如果只有一张纸,则自动选择第一个。
我已经尝试了以下代码:
import pandas as pd
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
df = pd.read_excel(file_path, sheet_name = 1)
df
这起作用,但是它不能为用户提供选择他们想要使用的Excel文件的能力。也许应该弹出一个新窗口(如果有多个表格),并要求用户选择哪些表格可用。如果只有一个纸,则应自动选择第一张纸。
I'm looking to create a Pandas data frame by asking the user to choose a file from their directory. In addition to choosing the file, though, I'm looking to also specify a specific sheet in the file. If only one sheet exists, then automatically choose the first one.
I have tried the code below:
import pandas as pd
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
df = pd.read_excel(file_path, sheet_name = 1)
df
This works, but it doesn't provide the user the ability to choose which sheet of the excel file they'd like to use. Perhaps a new window should pop up (if there's more than one sheet) and ask the user to select which sheet names are available. If only one sheet is available, then it should automatically choose the first sheet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)