我的熊猫数据框中有一些记录输出为NaT
。
即 Date_Refused_Final_Something_of_ICP=NaT,
这似乎破坏了我的脚本。我只想跳过发现为Nat的那几条记录,并继续其余的记录/脚本。
下面是完整的相关代码:
def get_excel_data(self):
"""Places excel data into pandas dataframe"""
excel_data = pandas.read_excel(self.find_file())
columns = pandas.DataFrame(columns=excel_data.columns.tolist())
excel_data = pandas.concat([excel_data, columns])
excel_data.columns = excel_data.columns.str.strip()
excel_data.columns = excel_data.columns.str.replace("/", "_")
excel_data.columns = excel_data.columns.str.replace(" ", "_")
num_valid_records = 0
for row in excel_data.itertuples():
mrn = row.MRN
if mrn in ("", " ", "N/A", None) or math.isnan(mrn):
print(f"Invalid record: {row}")
excel_data = excel_data.drop(excel_data.index[row.Index])
else:
num_valid_records += 1
print(f"Processing #{num_valid_records} records")
return self.clean_data_frame(excel_data)
def clean_data_frame(self, data_frame):
"""Cleans up dataframes"""
for col in data_frame.columns:
if "date" in col.lower():
data_frame[col] = pandas.to_datetime(data_frame[col],
errors='coerce', infer_datetime_format=True)
data_frame[col] = data_frame[col].dt.date
data_frame['MRN'] = data_frame['MRN'].astype(int).astype(str)
return data_frame
def get_mapping_data(self):
map_data = pandas.read_excel(config.MAPPING_DOC, sheet_name='main')
columns = pandas.DataFrame(columns=map_data.columns.tolist())
return pandas.concat([map_data, columns])
答案 0 :(得分:1)
我不确定您在哪里遇到这个问题,但是只要找到continue
,您就可以把它放进去:
if mrn in ("", " ", "N/A", None) or math.isnan(mrn):
print(f"Invalid record: {row}")
continue # <--- if it encounters any of the conditions above, it skips and goes to the next row in your excel_data