导入前检查文本文件中的数据框是否为空
Check if dataframe in text file empty before importation
我是 pandas 的新用户,我需要一些帮助。
我在一个文件夹中有 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))
(skiprows 是为了避免数据之前的文本,files_SE 是一个包含所有名称文件的列表)。
但我的问题是循环在 file_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*
有人为我提供解决方案吗?
非常感谢
您可以这样检查:
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)
我是 pandas 的新用户,我需要一些帮助。 我在一个文件夹中有 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))
(skiprows 是为了避免数据之前的文本,files_SE 是一个包含所有名称文件的列表)。
但我的问题是循环在 file_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*
有人为我提供解决方案吗? 非常感谢
您可以这样检查:
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)