0

I have list of dictionary like this:

collection = [{'item': ['Policy Master 1-2022-2023-P0003 - 5 Days - Plan 3', 'Policy Master 2-2022-2023-P0009 - 5 Days - Plan 3', 'Policy Master 3-2022-2023-P0012 - 5 Days - Plan 3', 'Policy Master 1-2022-2023-P0003 - 5 Days - Plan 2', 'Policy Master 2-2022-2023-P0009 - 5 Days - Plan 2'], 'Mar': [2800.0, 600.0, 600.0, 1000.0, 200.0]}, {'item': ['Policy Master 1-2022-2023-P0003 - 5 Days - Plan 3', 'Policy Master 2-2022-2023-P0009 - 5 Days - Plan 3', 'Policy Master 3-2022-2023-P0012 - 5 Days - Plan 3', 'Policy Master 1-2022-2023-P0003 - 5 Days - Plan 2'], 'Jun': [2800.0, 600.0, 600.0, 1000.0]}]

I want to item from show monthly data corresponding to it. If item was not present in previous month it should show NaN in previous month. How to do it in pandas? Also more months can be added in future. So it should consider all months dynamically.

2 Answers2

0

It appears you have a list of possible dataframes that can be merged together:

for i, x in enumerate(collection):
    if not i:
        df = pd.DataFrame(x)
    else:
        df = df.merge(pd.DataFrame(x), 'outer')

print(df)

Output:

                                                item     Mar     Jun
0  Policy Master 1-2022-2023-P0003 - 5 Days - Plan 3  2800.0  2800.0
1  Policy Master 2-2022-2023-P0009 - 5 Days - Plan 3   600.0   600.0
2  Policy Master 3-2022-2023-P0012 - 5 Days - Plan 3   600.0   600.0
3  Policy Master 1-2022-2023-P0003 - 5 Days - Plan 2  1000.0  1000.0
4  Policy Master 2-2022-2023-P0009 - 5 Days - Plan 2   200.0     NaN

Or:

from functools import reduce

reduce(lambda x, y: pd.merge(pd.DataFrame(x), pd.DataFrame(y), 'outer'), collection)
BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • It looks like a more efficient way of doing this may be found [here](https://stackoverflow.com/questions/38089010/merge-a-list-of-pandas-dataframes) – BeRT2me Jun 15 '22 at 06:37
  • Hey....this method will only if there is 2 months....what if more months added..is there a way to do it dynamically? – ramit_acube Jun 15 '22 at 09:52
  • @ramit_acube Are you just assuming it will only work on two months, or have you actually tried it with more months? It will work with an arbitrary number of months and is fully dynamic. – BeRT2me Jun 15 '22 at 16:35
  • i tried it..but data i was getting was wrong – ramit_acube Jun 16 '22 at 14:38
0

Continue and complete the previous code:

for i, x in enumerate(collection):
    if not i:
        df = pd.DataFrame(x)
    else:
        df = df.merge(pd.DataFrame(x), 'outer')
df_1 = df['item'].str.split("-", expand=True)
df = df.join(df_1)
df.drop(['item'], axis=1, inplace=True)

The input:

enter image description here