我正试图从excel文件中提取数据并将其传递到另一个新表中,但似乎文件目录是错误的。
import os
import pandas as pd
data_location = pd.read_excel(r"C:UsersbarışDesktopy2.xlsx")
desired_headings = ["Valuable Information"]
df_total = pd.DataFrame(columns=desired_headings)
for file in os.listdir(data_location):
df_file = pd.read_excel(data_location + file)
selected_columns = df_file.loc[:, desired_headings]
df_total = pd.concat([selected_columns,df_total],ignore_index=True)
df_total.to_excel("ValuableInformation.xlsx")
输出:
File "C:UsersbarışPycharmProjectspythonProject1Trial 8.py", line 8, in <module>
for file in os.listdir(data_location):
TypeError: listdir: path should be string, bytes, os.PathLike or None, not DataFrame
你有
data_location = pd.read_excel(r"C:UsersbarışDesktopy2.xlsx")
所以data_location是一个数据帧。然后是
for file in os.listdir(data_locat`enter code here`ion):
操作系统。Listdir需要一个目录,而不是一个数据框架,所以可以试试这样写
for file in os.listdir("C:UsersbarışDesktop")