简体   繁体   中英

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!

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM