1

I am using Pandas and want to add rows to an empty DataFrame with columns already established.

So far my code looks like this...

def addRows(cereals,lines):
    for i in np.arange(1,len(lines)):
        dt = parseLine(lines[i])
        dt = pd.Series(dt)
        print(dt)
    # YOUR CODE GOES HERE (add dt to cereals)
       cereals.append(dt, ignore_index = True)
    return(cereals)

However, when I run...

cereals = addRows(cereals,lines)
cereals

the dataframe returns with no rows, just the columns. I am not sure what I am doing wrong but I am pretty sure it has something to do with the append method. Anyone have any ideas as to what I am doing wrong?

brianhalperin
  • 63
  • 1
  • 6

1 Answers1

1

There are two probably reasons your code is not operating as intended:

  • cereals.append(dt, ignore_index = True) is not doing what you think it is. You're trying to append a series, not a DataFrame there.

  • cereals.append(dt, ignore_index = True) does not modify cereals in place, so when you return it, you're returning an unchanged copy. An equivalent function would look like this:

--

>>> def foo(a):
...    a + 1
...    return a
... 
>>> foo(1)
1

I haven't tested this on my machine, but I think you're fixed solution would look like this:

def addRows(cereals, lines):
    for i in np.arange(1,len(lines)):
        data = parseLine(lines[i])
        new_df = pd.DataFrame(data, columns=cereals.columns)
        cereals = cereals.append(new_df, ignore_index=True)
    return cereals

by the way.. I don't really know where lines is coming from, but right away I would at least modify it to look like this:

data = [parseLine(line) for line in lines]
cereals = cereals.append(pd.DataFrame(data, cereals.columns), ignore_index=True)

How to add an extra row to a pandas dataframe

You could also create a new DataFrame and just append that DataFrame to your existing one. E.g.

>>> import pandas as pd
>>> empty_alph = pd.DataFrame(columns=['letter', 'index'])
>>> alph_abc = pd.DataFrame([['a', 0], ['b', 1], ['c', 2]], columns=['letter', 'index'])
>>> empty_alph.append(alph_abc)
  letter  index
0      a    0.0
1      b    1.0
2      c    2.0

As I noted in the link, you can also use the loc method on a DataFrame:

>>> df = empty_alph.append(alph_abc)
>>> df.loc[df.shape[0]] = ['d', 3]  // df.shape[0] just finds next # in index
  letter  index
0      a    0.0
1      b    1.0
2      c    2.0
3      d    3.0
Community
  • 1
  • 1
Nick Brady
  • 6,084
  • 1
  • 46
  • 71