Skip to content Skip to sidebar Skip to footer

Adding Rows To Empty Dataframe With Columns

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(

Solution 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:

--

>>>deffoo(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

Post a Comment for "Adding Rows To Empty Dataframe With Columns"