'How to append empty row/column and how to use .empty() method properly?
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.
Solution 1:[1]
try:
def get_content_df(item):
if item is nan:
item = ""
content = item
if content is not None:
result_df = pd.DataFrame([content])
else:
result_df = pd.DataFrame([""])
return result_df
I this is ok !, no?
my_list = ["", ""]
my_list2 = [22, "dksjds"]
for j in my_list2:
#print(i, get_content_df(i))
res = get_content_df(j)
#print(res)
final_result_df3 = pd.concat([final_result_df3, res], ignore_index=True, axis=0)
even if the list is a series of nan, or ""s - it gets the result.
i.e. if list = [None] * 10, say it works...
hope this helps ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
