gpt4 book ai didi

python - 将新行附加到现有数据框

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:54 24 4
gpt4 key购买 nike

我试图将新记录插入到数据框中,但收到此错误 - DataFrame 构造函数未正确调用

这是我的代码,

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))

请帮我解决这个问题。

最佳答案

如果您需要添加单行:

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

注意:通常,首先将数据收集到列表列表中,然后调用 pd.DataFrame() 构造函数,通常会更好、更快、消耗更少的内存(这取决于......) >一次:

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

关于python - 将新行附加到现有数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47500081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com