7

I have a pandas dataframe that has been defined as empty and then I would like to add some rows to it after doing some calculations.

I have tried to do the following:

test = pd.DataFrame(columns=['Name', 'Age', 'Gender'])

if #some statement:

test.append(['James', '95', 'M'])

If I try to print and then append to test shows

print(test)

test.append(['a', 'a', 'a', 'a', 'a', 'a'])

print(test)

>>>

Empty DataFrame
Columns: [Name, Age, Gender]
Index: []
Empty DataFrame
Columns: [Name, Age, Gender]
Index: []

So clearly the line is not being added to the dataframe.

I want the output to be

Name | Age | Gender
James | 95 | M
user9940344
  • 574
  • 1
  • 8
  • 26

6 Answers6

10

Use append with dictionary as:

test = test.append(dict(zip(test.columns,['James', '95', 'M'])), ignore_index=True)

print(test)
    Name Age Gender
0  James  95      M
Space Impact
  • 13,085
  • 23
  • 48
2

You can pass as a Series:

test.append(pd.Series(['James', 95, 'M'], index=test.columns), ignore_index=True)

[out]

    Name Age Gender
0  James  95      M
Chris Adams
  • 18,389
  • 4
  • 22
  • 39
  • Will give it a try, do you know how efficient this would be compared to the loc method that is in pandas version 0.13 and above described here:https://stackoverflow.com/questions/19365513/how-to-add-an-extra-row-to-a-pandas-dataframe/19368360#19368360 – user9940344 Jul 08 '19 at 09:53
  • 1
    `append` is generally more computationally expensive than other operations - `concat` for example. From the [docs](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html) it may be better to append the new rows to a list, then use `concat`, than to append many rows iteratively. – Chris Adams Jul 08 '19 at 09:53
1

Try to append it as dictionary:

>>> test = test.append({'Name': "James", "Age": 95, "Gender": "M"}, ignore_index=True)
>>> print(test)

Outputs:

    Name Age Gender
0  James  95      M
Gahan
  • 4,075
  • 4
  • 24
  • 44
  • Will give it a try, do you know how efficient this would be compared to the loc method that is in pandas version 0.13 and above described here:https://stackoverflow.com/questions/19365513/how-to-add-an-extra-row-to-a-pandas-dataframe/19368360#19368360 – user9940344 Jul 08 '19 at 09:52
1

Append function will convert list into DataFrame,it automatic generation columns[0],but test have columns=['Name', 'Age', 'Gender'].And append Will not change test.What I said may be confusing,running the following code might make you understand.

import pandas as pd

#Append function will convert list into DataFrame,and two dataframe object should have the same column
data = pd.DataFrame(['James', '95', 'M'])
print(data)

#right code
test = pd.DataFrame(columns=['Name', 'Age', 'Gender'])
test = test.append(pd.DataFrame([['James', '95', 'M']],columns=['Name', 'Age', 'Gender']))
print(test)
wangtianye
  • 306
  • 1
  • 5
0

First, the append method does not modify the DataFrame in place but returns the modified (appended version).

Second, the new row passed should be either a DataFrame, either a dict/Series, or either a list of these.

#using dict
row = {'Name': "James", "Age": 95, "Gender": "M"}
# using Series
row = pd.Series(['James', 95, 'M'], index=test.columns))

Try print( test.append(row) ) and see the result.

What you need is to save the return value of test.append as the appended version, you can save it with the same name if you don't care about the preceding version, it gives us:

test = test.append( row )
Nick Skywalker
  • 1,027
  • 2
  • 10
  • 26
  • I tried this and it adds each one as a new row so it gives James, 95, M going down the name column with the rest blanks. – user9940344 Jul 08 '19 at 09:55
  • Yes, I forgot you need to pass a `Dataframe` or dict or `Series` instead of just a list, I modified the answer to make it complete using other ones' suggestions. – Nick Skywalker Jul 08 '19 at 10:02
0

Try this,

>>> test.append(dict(zip(test.columns,['James', '95', 'M'])), ignore_index=True)

    Name Age Gender
0  James  95      M
shaik moeed
  • 5,300
  • 1
  • 18
  • 54