0

I am a beginner trying to append rows of data via a for loop to a dataframe but keep getting

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "Series"

Here is the relevant code:

myCols = ['Ticker', 'Name', 'Age', 'Height']  
finalData = pd.DataFrame(columns=myCols)
 listOfPerson=[{Name: 'John', Age: '27', Height '115'},{Name: 'Jill', Age: '25', Height '110'}]
 for person in listOfPerson:
    finalData.append(pd.Series([
        person.name,
        person.age,
        person.height,
        'N/A'
    ], index=myCols
    ), ignore_index = True, axis=1)

Any tips are appreciated and let me know if any further info is needed.

I have tried reading up on other forum posts and have tried .concat() but it also tells me it does not exist. I cannot seem to figure it out. Perhaps it is a datatype error and the documentation online is just confusing.

AbbasK
  • 3
  • 2
  • Getting this error, you might be using an old version of pandas. What version are you running? – OCa Aug 27 '23 at 21:34
  • Aslo, welcome to SO! Please have a look into https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – OCa Aug 27 '23 at 21:35
  • `append` is no longer available in the Pandas v2. But `concat` can be used. Please provide a reproducible code and data example demonstrating your problem with concat. – user19077881 Aug 27 '23 at 21:47
  • Don't grow a DataFrame in a loop, the behavior is quadratic. Just use the DataFrame constructor directly: `df = pd.DataFrame(listOfPerson)`. Much easier, much more efficient... – mozway Aug 28 '23 at 05:15

2 Answers2

0

The other answer already shows how to fix your code.

Generally append would be slow, because on each append, it needs to make a complete copy of the dataframe. For performance I recommend first creating the list (listOfPerson) in memory and then constructing the dataframe all at once, like this:

myCols = ['Ticker', 'Name', 'Age', 'Height']
finalData = pd.DataFrame(columns=myCols)
listOfPerson = [{'Name': 'John', 'Age': '27', 'Height': '115'}, {'Name': 'Jill', 'Age': '25', 'Height': '110'}]

finalData = pd.DataFrame(listOfPerson, columns=myCols)
print(f"{finalData=}")
-1

The AttributeError you're encountering ('DataFrame' object has no attribute 'append') is due to how the append() method works in Pandas. It doesn't modify the DataFrame in place; instead, it returns a new DataFrame with the appended data. Here's how you can modify your code to fix this issue:

import pandas as pd

myCols = ['Ticker', 'Price', 'One-Yr Price Return', 'Number of Shares to purchase']
finalData = pd.DataFrame(columns=myCols)

for symbol in symbolStrings[0].split(','):
    new_row = pd.Series([
        symbol,
        latestData[symbol].vwap,
        'na',
        'na'
    ], index=myCols)
    finalData = finalData.append(new_row, ignore_index=True)

print(finalData)

In this corrected version, we're creating a new row using a pd.Series object and then using the append() method to add the new row to the finalData DataFrame. It's important to assign the result of append() back to the finalData variable to update the DataFrame.

Additionally, please keep in mind that having spaces in column names (e.g., Number of Shares to purchase) can sometimes lead to confusion. It's generally a good practice to avoid spaces in column names. You might consider using underscores or camel case instead.

Remember to adjust symbolStrings and latestData according to your actual data sources.


Hopefully, this solution helps you resolve the issue. If you have any further questions or need additional assistance, feel free to ask!

  • Great explanation thank you! I edited my code as someone said I should create a reproducable example so if you want to edit your solution so that it makes more sense to other people who see this feel free to. – AbbasK Aug 27 '23 at 22:21
  • This is not a good pandas approach. Never grow a DataFrame in a loop. Also, now `append` was removed from the API. The correct way is to use the DataFrame constructor directly: `df = pd.DataFrame(listOfPerson)` – mozway Aug 28 '23 at 05:14
  • 1
    https://stackoverflow.com/help/gpt-policy – DavidW Aug 28 '23 at 06:05