Adding a dictionary to a row in a Pandas DataFrame using concat in pandas 1.4

Question:

After updating to pandas 1.4, I now receive the following warning when using frame.append to append a dictionary to a Pandas DataFrame.

FutureWarning: The frame.append method is deprecated and will be
removed from pandas in a future version. Use pandas.concat instead.

Below is the code. This still works, though I would like to resolve the warning.

report = report.append({
                "period":period,
                "symbol":symbol,
                "start_date":start_date,
                "start_price":start_price,
                "start_market_cap":start_market_cap,
                "end_date":end_date,
                "end_price":end_price,
                "end_market_cap":end_market_cap,
                "return":return_
            },ignore_index=True)

I have updated the code to the below, which kicks a different warning:

report = pd.concat([report,{
                "period":period,
                "symbol":symbol,
                "start_date":start_date,
                "start_price":start_price,
                "start_market_cap":start_market_cap,
                "end_date":end_date,
                "end_price":end_price,
                "end_market_cap":end_market_cap,
                "return":return_
            }],ignore_index=True)

TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid

2 questions:
Is the first warning wrong?
What is the pandas 1.4 way to achieve this?
Thanks.

Asked By: mattblack

||

Answers:

Use loc to assign a single row value:

report.loc[len(report)] = {"period":period,
                           "symbol":symbol,
                           "start_date":start_date,
                           "start_price":start_price,
                           "start_market_cap":start_market_cap,
                           "end_date":end_date,
                           "end_price":end_price,
                           "end_market_cap":end_market_cap,
                           "return":return_
                          }
Answered By: user7864386
report = pd.concat([report, pd.DataFrame([mydict])], ignore_index=True)

where mydict is the row you want added.

Unlike append, concat does not support dicts directly.

append was deprecated in pandas 1.4.0

Answered By: fantabolous

The answer posted by @fantabolous will work even if the df is empty with no columns.

import pandas as pd 
df =  pd.DataFrame(columns=[])
data_dict = {"a":0,"b":2742,"c":13.38867188,"d":0}
df = pd.concat([df, pd.DataFrame([data_dict])], ignore_index=True)
df
Answered By: Satish Gaurav
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.