在导入之前检查文本文件中的数据框是否为空

时间:2017-04-25 10:14:22

标签: python pandas dataframe is-empty

我是熊猫的新用户,我需要一些帮助。 我在一个文件夹中有10个txt文件:

file_date1_1.txt
...
file_date1_5.txt
file_date2_1.txt
...
file_date2_5.txt

每个文件都具有相同的设计:

- text to explain the file
- datas. 

要导出一个文件,请执行以下代码:

Data_Frame = pd.read_csv(Location,  delimiter=r'\s+', index_col=False, header = None, skiprows=11)

我想做的是做一个循环来制作数据帧列表。 所以我这样做了:

for i in range(0,len(files_SE)):
    date.append(files_SE[i][0:7])
    hour.append(files_SE[i][8:13])
    Location.append('\\'.join([adress,files_SE[i]]))
    SE_df.append(pd.read_csv(Location[i],  delimiter=r'\s+', index_col=False, header = None, skiprows=11))

(跳过是为了避免数据之前的文本,files_SE是一个包含所有名称文件的列表)。

但我的问题是循环停在f ile_date1_5.txt因为没有数据(它是空的)。 我想要的是做出一个像

这样的条件
if (pd.read_csv(Location[i],  delimiter=r'\s+', index_col=False, header = None, skiprows=11)).empty:
    *do nothing*
else:
    *do the importation of the dataframe*

有没有人为我提供解决方案? 非常感谢

1 个答案:

答案 0 :(得分:1)

您可以像这样进行检查:

Current_Data = pd.read_csv(Location[i],  delimiter=r'\s+', index_col=False, header = None, skiprows=11)
if not Current_Data.empty:
    SE_df.append(Current_Data)
相关问题