Adding rows in dataframe with a condition in list

Question:

I am trying to add a row with the condition but was having difficulty achieving this.

Currently, I have pandas dataframes in a list that looks like following

enter image description here

enter image description here

The objective is to add a row with the condition that I want to add a row with a fixed number for ‘ID’ and increase the month by 3.

For example, for this[1] I want it to add rows that look like following

ID | month | num

6 | 0 | 5

6 | 3 | NaN

6 | 6 | 4

6 | 9 | NaN

6 | 12 | 3

6 | 36 | 1

I am trying to create a function that takes the index of the list (so it would be an actual dataframe), the max number of the month of that dataframe, and month I want it to be incremented by (3), which would look like

def add_rows(df, max_mon, res):
  if max_mon > res:
     add rows with fixed ID and NaN num
     skip the month that already exist
  

final = []

for i in range(len(this)):
  final.append(add_rows(this[i], this[i]['month'].max(), 3))
   

I have tried to insert rows but I did not manage to get it work.

The toy data

d = {'ID':[5,5,5,5,5], 'month':[0,6,12,24,36], 'num':[5,4,3,2,1]}
tempo = pd.DataFrame(data = d)
d2 = {'ID':[6,6,6,6,6], 'month':[0,6,12,18,36], 'num':[5,4,3,2,1]}
tempo2 = pd.DataFrame(data = d2)
this = []
this.append(tempo)
this.append(tempo2)

I would really appreciate if I could get help on building the function!

Asked By: reksapj

||

Answers:

You can use:

for i, df in enumerate(this):
    this[i] = (df
        .set_index('month')
        .groupby('ID')
        .apply(lambda x: x.drop(columns='ID')
        .reindex(range(x.index.min(), x.index.max()+3, 3))
                    )
        .reset_index()[df.columns]
        )

Updated this:

[    ID  month  num
 0    5      0  5.0
 1    5      3  NaN
 2    5      6  4.0
 3    5      9  NaN
 4    5     12  3.0
 5    5     15  NaN
 6    5     18  NaN
 7    5     21  NaN
 8    5     24  2.0
 9    5     27  NaN
 10   5     30  NaN
 11   5     33  NaN
 12   5     36  1.0,
     ID  month  num
 0    6      0  5.0
 1    6      3  NaN
 2    6      6  4.0
 3    6      9  NaN
 4    6     12  3.0
 5    6     15  NaN
 6    6     18  2.0
 7    6     21  NaN
 8    6     24  NaN
 9    6     27  NaN
 10   6     30  NaN
 11   6     33  NaN
 12   6     36  1.0]
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.