[英]__str__ returning one giant string instead of individual values. New to Python
class CovidRecord:
def __init__(self, pruid, prname, prnameFR, date, update, numcomf, numprob, numdeaths, numtotal):
self.pruid = pruid
self.prname = prname
self.prnameFR = prnameFR
self.date = date
self.update = update
self.numconf = numcomf
self.numprob = numprob
self.numdeaths = numdeaths
self.numtotal = numtotal
def __str__(self):
return self.pruid + self.prname + self.prnameFR + self.date + self.update + self.numconf + self.numprob + self.numdeaths + self.numtotal`
def write_to_dataframe(self):
dataframe = pd.read_csv(r'C:\Users\jakev\Downloads\covid19-download.csv')
dataframetop = dataframe.head(6)
print('Please insert information to add')
pruid = input('Province ID: ')
prname = input('Province name: ')
prnameFR = input('Province name in French: ')
date = input('Date: ')
update = input('When was it updated: ')
numcomf = input('Confirmed cases: ')
numprob = input('Probable cases: ')
numdeaths = input('Number of deaths ')
numtotal = input('Total number')
newrecord = CovidRecord(pruid, prname, prnameFR, date, update, numcomf, numprob, numdeaths, numtotal)
dfobj = pd.DataFrame(newrecord, columns = ['prvid', 'prname', 'prnameFR', 'date', 'update', 'numcomf', 'numprob'
, 'numdeaths', 'numtotal', 'newrecord'], index=['1'])
newdataframe = dataframetop.append(dfobj, ignore_index = True)
print(newdataframe)
CovidRecord 是我創建的記錄 object 以將其他記錄加載到 dataframe 中。 當我在終端中輸入對輸入的響應時,它會在每一列下打印出一個巨大的字符串,其中包含我的每個響應,而不是將它們分成各自的合法列。 我怎樣才能做到,當我輸入“prvname”時,它只將我的答案放在省名下等等? 下面是我得到的 output 的示例。
pruid prname prnameFR date
0 35.0 Ontario Ontario 2020-01-31
1 59.0 British Columbia Colombie-Britannique 2020-01-31
6 NaN 23OntarioOntario2021-02-182021-02-182334278 23OntarioOntario2021-02-182021-02-182334278 23OntarioOntario2021-02-182021-02-182334278
底部記錄是我嘗試插入的記錄。 它將我對每個輸入的所有答案都放入 1 個字符串中,然后將一個巨大的字符串放入每一列。
我錯過了什么?
謝謝。
str返回一個巨大的字符串
您自己將它們與以下行放在一起:
return self.pruid + self.prname + self.prnameFR + self.date + self.update + self.numconf + self.numprob + self.numdeaths + self.numtotal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.