简体   繁体   English

如何从位于指定文件夹中的一个文件(具有任何文件名)创建一个 Pandas 数据框?

[英]How to create a pandas dataframe from one file (with any file name) located in a specified folder?

What's the best way to create a pandas dataframe from one file with any file name located in a specified folder?从位于指定文件夹中的任何文件名的文件创建 Pandas 数据框的最佳方法是什么?

I have used pathlib and it's not quite working as the output dataframe is not giving me anything.我使用了 pathlib 并且它不太工作,因为输出数据框没有给我任何东西。

from pathlib import Path
import pandas as pd

pth = r'C:\Users\HP\Desktop\IBM\New folder'
fle = Path(pth).glob('*.tsv')

someDf = pd.DataFrame(fle)
someDf

Edit:编辑:

I also tried doing the below, but the output dataframe combines all columns into one column separated by a backward slash.我也尝试执行以下操作,但输出数据帧将所有列合并为一列,以反斜杠分隔。 How do I fix this?我该如何解决?

from pathlib import Path
import pandas as pd

pth = r'C:\Users\HP\Desktop\IBM\New folder'
fle = Path(pth).glob('*.tsv')

dfs = []
for filename in fle:
    dfs.append(pd.read_csv(filename))

dfs1 = pd.concat(dfs)
dfs1.head()

在此处输入图片说明

The way I did this seems complicated.我这样做的方式似乎很复杂。 Is there an easier way to do this?有没有更简单的方法来做到这一点?

Please try:请尝试:

from pathlib import Path
import pandas as pd
import os
pth = r'C:\Users\HP\Desktop\IBM\New folder'
for file_ in os.listdir(pth):
    h=os.path.join(pth, file_)
    #print (h)
someDf = pd.read_csv(h)
someDf

Try尝试

from glob import glob
files = glob('C:\Users\HP\Desktop\IBM\New folder\*.tsv')
if len(files) == 1:
    dfs = pd.read_csv(files[0], sep='\t')
else:
    dfs = pd.concat([pd.read_csv(file, sep='\t') for file in files])

The solution I found for this is as below.我为此找到的解决方案如下。 I missed the sep parameter in pd.read_csv() .我错过了pd.read_csv()sep参数。

from pathlib import Path
import pandas as pd

pth = r'C:\Users\HP\Desktop\IBM\New folder'
fle = Path(pth).glob('*.tsv')

dfs = []
for filename in fle:
    dfs.append(pd.read_csv(filename, sep='\t'))

dfs1 = pd.concat(dfs)
dfs1.head()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将位于包含模块的文件夹之外的 Excel 文件读入 Pandas DataFrame - Read Excel file that is located outside the folder containing the module into Pandas DataFrame 如何将多个CSV文件从文件夹读取到以数据框名称作为文件名的熊猫中 - how to read multiple CSV files from folder into pandas with dataframe name as file name 将所有 csv 文件合并到一个文件夹中,并在 Python 中使用部分文件名向 Pandas 数据框添加一个新列 - Merge all the csv file in one folder and add a new column to pandas dataframe with partial file name in Python 如何从熊猫中的多个数据框创建Csv文件,并以数据框的名称作为每一列的标题? - How to create a Csv file from multiple dataframes in pandas with the name of the dataframe as a header of each column? 如何在 pandas 的帮助下创建 function 以读取文件夹中的所有文件并为每个文件创建不同的 dataframe? - How to create function with help of pandas to read all file in a folder and create different dataframe for each file? Python Selenium with Chrome,如何下载到指定文件名的指定文件夹 - Python Selenium with Chrome, how to download to a specified folder with specified file name 从 Pandas 数据框中按文件名提取列 - Extract columns by file name from Pandas dataframe 从压缩文件夹中的文件夹中读取 txt 文件 pandas dataframe - Reading in txt file as pandas dataframe from a folder within a zipped folder 如何从没有通用分隔符的文件创建熊猫数据框? - How to create a pandas dataframe from a file with no common separator? Python - 如何从 Pandas Z6A8064B5DF47945557DZ0553 组创建 JSON 嵌套文件? - Python - How to create a JSON nested file from a Pandas dataframe and group by?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM