You can use something like this as read_csv does not save the file_name.
Using glob will give you the ability to put wildcards or regex for all the CSV files on that folder for reading.
import glob
data = {}
for filename in glob.glob("/path/of/the/csv/files/*.csv"):
data[filename.split("/")[-1].split(".")[0]] = pd.read_csv(filename)
for key, value in data.items():
print(key)
print(value.info())
print("\n\n")
filename.split("/")[-1].split('.')[0]
The above line may look complicated but it just split the file_name 2 times.
filename.split("/")[-1].split('.')[0]
使用pandas
read_csv
函数时,会得到一个不包含文件名的数据帧。因此,解决方案是将.csv
的名称存储在变量中,然后打印它。您可以在pandas.DataFrame Documentation中检查熊猫数据帧pandas.read_csv()
方法接受File
对象(实际上是任何具有read()
方法的类似文件的对象)并且
File
类有一个name
对象,该对象具有打开的文件的名称我认为这段代码和情况完全没有意义,因为您事先已经知道文件名,但为了完整起见,现在开始:
相关问题 更多 >
编程相关推荐