2

I have an empty dataframe and I want to loop over an API call and input data into it based on the stock symbol in my spreadsheet.

final_dataframe = final_dataframe.append(
        pd.Series(
        [
            symbol,
            data[symbol]['price'],
            data[symbol]['stats']['year1ChangePercent'], 
            'N/A'
        ],
        index = my_columns),
            ignore_index = True
        )

As above, I currently use the append method to add the data to my dataframe, but as append is depreciating in pandas 1.4.0, I want to know how to re write the above using concat. I current have this but it does not work:

final_dataframe = pd.concat([final_dataframe,
        pd.Series(
        [
            symbol,
            data[symbol]['price'],
            data[symbol]['stats']['year1ChangePercent'], 
            'N/A'
        ],
        index = my_columns),
            ignore_index = True
                                    ])
semblable
  • 773
  • 1
  • 8
  • 26
Will Triccas
  • 21
  • 1
  • 2
  • Is it the placement of the last `]` in your code? Looks like it should be moved to after the `)`. Hard to tell for sure without the output from your code. – Professor Pantsless Feb 03 '22 at 21:49
  • Good Q&A on this subject here [good-alternative-to-pandas-append](https://stackoverflow.com/questions/70837397/good-alternative-to-pandas-append-method-now-that-it-is-being-deprecated) – InnocentBystander Dec 29 '22 at 21:39

3 Answers3

0

Not sure concat supports concatenating a DataFrame with a Series so maybe try .to_frame(), and I've also moved a closing bracket:

final_dataframe = pd.concat([final_dataframe,
    pd.Series(
    [
        symbol,
        data[symbol]['price'],
        data[symbol]['stats']['year1ChangePercent'], 
        'N/A'
    ],
    index = my_columns).to_frame()],
        ignore_index = True
                                )

Using .loc is another option.

Anynamer
  • 334
  • 1
  • 6
0

Replace pd.Series with pd.DataFrame:

final_dataframe = pd.concat([final_dataframe, pd.DataFrame({
     final_dataframe.columns[0]: [symbol],
     final_dataframe.columns[1]: [data[symbol]['price']],
     final_dataframe.columns[2]: [data[symbol]['stats']['year1ChangePercent']],
     final_dataframe.columns[3]: ['N/A']})], ignore_index=True)
0

You can still do it with a dictionary but the value needs to be in a list then placed in a temporary pandas.DataFrame

append_dict = {'col0':val0,'col1':val1}
temp = pd.DataFrame({k:[v] for k,v in append_dict.items()})
df = pd.concat([df,temp],ignore_index=True)
mbauer
  • 116
  • 1
  • 4