[英]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.