[英]DataFrame error of constructor not properly called
so I have a dataframe which I created using my code, when I use a for loop to loop through the columns and get different results to create another dataframe I get this error DataFrame constructor not properly called!所以我有一个使用我的代码创建的 dataframe,当我使用 for 循环遍历列并获得不同的结果来创建另一个 dataframe 我得到这个错误 ZBA834BA059A9A379459C112175EB8 构造函数没有正确调用!
here is my code这是我的代码
#get all trafficlights ids with traci
tlsID= traci.trafficlight.getIDList()
print(tlsID)
# loop through the trafficlights ids and save them to dataframe
for i in tlsID:
df = pd.DataFrame(tlsID, columns=[i])
i =+1
# remane the coulmn to any name you want
df.columns= ['tlsID']
# export your dataframe to csv file
df.to_csv('tlsID.csv', index=False)
# convert dataframe to strings
tlsID_df= df['tlsID'].astype(str)
# create an empty list to append new items to it
df3=[]
df4=[]
# loop through your dataframe
for i in tlsID_df:
# get all lanes controlled by trafficlights (incoming lanes)and append to your list
controlled_lanes= traci.trafficlight.getControlledLanes(i)
appended = pd.DataFrame(controlled_lanes)
df3.append(appended)
i =+1
# put a name your column to use it later
col1= ['incoming']
# transfer your list to a dataframe
lanes= pd.concat(df3, ignore_index= True)
# remane your column
lanes.columns= col1
# drop duplicated rows
lanes= lanes.drop_duplicates()
# reset your index
lanes = lanes.reset_index(drop=True)
lanes.to_csv('incoming_links.csv', index=False)
lanes_df= lanes['incoming'].astype(str)
for i in lanes_df:
# get all lanes controlled by trafficlights (incoming lanes)and append to your list
lane= traci.lane.getLength(i)
append = pd.DataFrame(lane)
df4.append(append)
i =+1
# put a name your column to use it later
col2= ['len']
# transfer your list to a dataframe
lanes_len= pd.concat(df4, ignore_index= True)
# remane your column
lanes_len.columns= col2
# drop duplicated rows
lanes_len= lanes_len.drop_duplicates()
# reset your index
lanes_len = lanes_len.reset_index(drop=True)
# export your dataframe to csv file
lanes_len.to_csv('lanes_len.csv', index=False)
my error is in the second for loop我的错误在第二个 for 循环中
for i in lanes_df:
# get all lanes controlled by trafficlights (incoming lanes)and append to your list
lane= traci.lane.getLength(i)
append = pd.DataFrame(lane)
df4.append(append)
i =+1
this function - traci.lane.getLength(i) - returns float这个 function - traci.lane.getLength(i) - 返回浮点数
what possibly could be the problem, as I have already used it before and it worked fine可能是什么问题,因为我之前已经使用过它并且效果很好
so I figured it out, the issue was that my code returns float and I cannot create dataframe out of floats所以我想通了,问题是我的代码返回浮点数,我无法用浮点数创建 dataframe
so I did this所以我做了这个
for i in lanes_df:
# get all lanes controlled by trafficlights (incoming lanes)and append to your list
lane= traci.lane.getLength(i)
append = df4.append(lane)
i =+1
lanes_len = pd.DataFrame(np.array([df4]).T)
lanes_len.columns= ['len']
I appended all values to my list first, then with using numpy array I was able to create my data frame (note I used transpose to append each value in a separate row)我首先将所有值附加到我的列表中,然后使用 numpy 数组我能够创建我的数据框(注意我使用转置到 append 每个值在单独的行中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.