I'm writing a function which takes item from a list (such as item_x in [item_1, item_2, item_3, ..., item_n]) and returns dataframe_i (can be empty).
I want to concatenate the returned dataframes into a single final_result_df, which also has the same length as the list (even all returned dataframes are empty). I need to check if the final_result_df is empty.
Here are my codes:
def get_content_df(item):
content = do_something()
if content is not None:
result_df = pd.DataFrame([content])
else:
result_df = pd.DataFrame([""]) # This line is my problem
return result_df
my_list = [item_1, item_2, item_3, ..., item_n]
final_result_df = pd.DataFrame()
for item in my_list:
result_df_i = get_conetent_df(item)
final_result_df = pd.concat([final_result_df, result_df_i], ignore_index=True, axis=0)
print(final_result_df.empty)
The problem happens when all result_df_i fall into the condition that content is None is True.
If I use
result_df = pd.DataFrame([""]), thefinal_result_dfhas the shape I want, but the.emptymethod returns the wrong value.If I use
result_df = pd.DataFrame(), thefinal_result_dfdoes not have the shape I want, but the.emptymethod returns the correct value.
I believe the first condition must have some quick fix, but I'm not sure how to achieve that. I know this might not be how pandas was supposed to be used, but I haven't came up with a better idea. Any advice on other methods is also wellcomed.