I want to use Pandas add operation to replace defaultdict(list) functional. If an index exists only in one dataframe, the result will be NaN. However, I hope the result is the existing list. For example:
In [3]: x = pd.DataFrame({'fnm': [[t] for t in 'abcdef']}, index=[0, 1, 2, 3, 4, 5]) In [4]: x Out[4]: fnm 0 [a] 1 [b] 2 [c] 3 [d] 4 [e] 5 [f] In [5]: y = pd.DataFrame({'fnm': [[t] for t in 'abcdef']}, index=[0, 2, 3, 4, 5, 6]) In [6]: y Out[6]: fnm 0 [a] 2 [b] 3 [c] 4 [d] 5 [e] 6 [f] In [7]: x + y Out[7]: fnm 0 [a, a] 1 NaN 2 [c, b] 3 [d, c] 4 [e, d] 5 [f, e] 6 NaN
I expect the result is
0 [a, a] 1 [b] 2 [c, b] 3 [d, c] 4 [e, d] 5 [f, e] 6 [f]
https://stackoverflow.com/questions/65838398/pandas-how-to-add-two-dataframe-with-list-element-ignoring-nan January 22, 2021 at 10:07AM
没有评论:
发表评论