Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas append() will be deprecated and I can't convert a specific df.append() to df.concat()

I have a current iteration to fill new rows to a dataframe based on new series created:

for i in range (x):
    nextMonth = df.index[-1] + DateOffset(months=1)
    newRow = pd.Series({'col_1':None,'col_2':1}, name=nextMonth)
    df = df.append(newRow)

This works fine. New rows are created, on correct df columns (col_1 and col_2) and I have a correct nextMonth named index on the df (2022-02-01 on date).

              col_1  col_2
date
1994-07-01  0.0684   7.177511
1994-08-01  0.0186   6.718000
1994-09-01  0.0153   6.595327
1994-10-01  0.0262   6.495939
1994-11-01  0.0281   6.330091
...            ...        ...
2021-10-01  0.0125   1.035140
2021-11-01  0.0095   1.022360
2021-12-01  0.0073   1.012739
2022-01-01  0.0054   1.005400
2022-02-01     NaN   1.000000 -----> series added

Note that I’m using the series named indexes to match with the df columns, and I´m also using the series name to use it on the final df as a named index (nextMonth).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Since df.append() will be deprecated, I´m struggling to perform the same instructions using df.concat().

>Solution :

By slightly reworking your loop, you could make it a dict comprehension to build a dictionary; construct a DataFrame with it; then use pd.concat to concatenate it to df. For example, if x=3:

x = 3
df = (pd.concat((df, pd.DataFrame.from_dict(
                                {df.index[-1] + DateOffset(months=i+1): {'col_1':np.nan, 'col_2':1} 
                                 for i in range(x)}, orient='index')))
      .rename_axis(index=df.index.name))

Output:

             col_1     col_2
date                        
1994-07-01  0.0684  7.177511
1994-08-01  0.0186  6.718000
1994-09-01  0.0153  6.595327
1994-10-01  0.0262  6.495939
1994-11-01  0.0281  6.330091
2021-10-01  0.0125  1.035140
2021-11-01  0.0095  1.022360
2021-12-01  0.0073  1.012739
2022-01-01  0.0054  1.005400
2022-02-01     NaN  1.000000
2022-03-01     NaN  1.000000
2022-04-01     NaN  1.000000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading