所以基本上我想要的是使用这个功能:
def __init__(self, myTuple: tuple):
for i in myTuple:
print(i[1])
self.dzien_tyg = i[1]
self.godz_rozp = i[2]
self.ilosc_godz = i[3]
self.czestatliwosc = i[4]
self.id_prowadzacego = i[5]
self.id_sali = i[6]
self.id_przedmiotu = i[7]
self.rodzaj = i[8]
self.nr_grupy = i[9]
self.id_studia = i[10]
self.nr_semetru = i[11]
self.id_specjalnosci = i[12]
使用此代码:
for row in df.iterrows():
print(row)
object = Grupa(*row)
我的pycharms说:
TypeError: __init__() takes 2 positional arguments but 3 were given
" row"寻找:
(0, dzien 1
godz 1
ilosc 2
tyg 0
id_naucz 52
id_sala 79
id_prz 13
rodz W
grupa 1
id_st 13
sem 1
id_spec 0
Name: 0, dtype: object)
我无法弄清楚pycharms在哪里看到这3个参数以及如何解决它
答案 0 :(得分:1)
尝试将*row
替换为row[1]
:
for row in df.iterrows():
print(row)
object = Grupa(row[1])
当你iterrows()
时,从print中可以看到的每一行都是一个两元素元组,第一个元素是行索引,第二个元素是一个包含实际数据的Series对象,您使用*row
将索引和系列对象都传递给构造函数。但是从__init__
定义来看,似乎只需要行对象。
如果每一行都是一个对象,那么__init__
中不需要for循环,只需对原始代码进行最小的更改:
def __init__(self, myTuple: tuple):
i = myTuple
print(i[1])
self.dzien_tyg = i.iloc[0] # be careful here when you index the Series object with
self.godz_rozp = i.iloc[1] # integers, use iloc to access elements and also note
self.ilosc_godz = i.iloc[2] # the indices are zero based
...
或者,您可以使用实际索引更安全地访问:
def __init__(self, myTuple: tuple):
i = myTuple
print(i['dzien'])
self.dzien_tyg = i['dzien']
self.godz_rozp = i['godz']
self.ilosc_godz = i['ilosc']
...