簡體   English   中英

DataFrame 構造函數未正確調用錯誤

[英]DataFrame error of constructor not properly called

所以我有一個使用我的代碼創建的 dataframe,當我使用 for 循環遍歷列並獲得不同的結果來創建另一個 dataframe 我得到這個錯誤 ZBA834BA059A9A379459C112175EB8 構造函數沒有正確調用!

這是我的代碼

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

我的錯誤在第二個 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

這個 function - traci.lane.getLength(i) - 返回浮點數

可能是什么問題,因為我之前已經使用過它並且效果很好

所以我想通了,問題是我的代碼返回浮點數,我無法用浮點數創建 dataframe

所以我做了這個

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']

我首先將所有值附加到我的列表中,然后使用 numpy 數組我能夠創建我的數據框(注意我使用轉置到 append 每個值在單獨的行中)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM