Appending a new row to an existing dataframe

I am trying to insert a new record to a data frame and am getting this error - DataFrame constructor not Properly called.

This is my code,

import pandas as pd

#defining the dataframe
dfHeader = { 'Name': [], 'Daily': [],'Weekly': [],'Monthly': [],'Yearly': [],
             'Area': [], 'Latitude':[] , 'Longitude':[] ,'Amenity': [] }

dFrame = pd.DataFrame(dfHeader)
print(dFrame)
Name = "NA"
DailyPrice = "NA"
WeeklyPrice = "NA"
MonthlyPrice = "NA"
YearlyPrice = "NA"
Area = "NA"
latitude = "NA"
longitude = "NA"
Amenity = "NA"

row = (Name, DailyPrice, WeeklyPrice, MonthlyPrice, YearlyPrice, Area, latitude, longitude, Amenity)

print(row)

dFrame = dFrame.append(pd.DataFrame(row))

Kindly help me with this.

Upvotes: 1

Views: 550

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

If you need to add a single row:

In [136]: dFrame.loc[len(dFrame)] = row

In [137]: dFrame
Out[137]:
  Amenity Area Daily Latitude Longitude Monthly Name Weekly Yearly
0      NA   NA    NA       NA        NA      NA   NA     NA     NA

NOTE: usually it's much better, faster, less memory consuming (it depends...) to collect the data first into list of lists and then to call pd.DataFrame() constructor once:

In [138]: data = [row, np.arange(9)]

In [139]: cols = 'Amenity Area Daily Latitude Longitude Monthly Name Weekly Yearly'.split()

In [140]: df = pd.DataFrame(data, columns=cols)

In [141]: df
Out[141]:
  Amenity Area Daily Latitude Longitude Monthly Name Weekly Yearly
0      NA   NA    NA       NA        NA      NA   NA     NA     NA
1       0    1     2        3         4       5    6      7      8

Upvotes: 1

Sebastian Mendez
Sebastian Mendez

Reputation: 2971

You can't create a DataFrame from a tuple. You can, however, create a Series from a tuple, and have the indicies be the columns of dFrame:

dFrame.append(pd.Series(row, index=dfHeader.keys()), ignore_index=True)

Upvotes: 0

Related Questions