2 回答

TA貢獻1828條經(jīng)驗 獲得超3個贊
我會創(chuàng)建一個列表并將所有數(shù)據(jù)幀添加到循環(huán)中的列表中,然后在最后使用 pd.concat:
results = []
for i in range(4000):
try:
shape_json = json.loads(region_shape[i])
file_name = file_name_nuclei[i]
x_val = shape_json["x"]
y_val = shape_json["y"]
width_val = shape_json["width"]
height_val = shape_json["height"]
path = '/home/values/' + str(file_name)
x1 = x_val
y1 = y_val
x2 = x_val + width_val
y2 = y_val + height_val
d = {'col1': [path], 'col2': [x1], 'col3': [y1], 'col4': [x2], 'col5': [y2], 'col5': ['nucleus']}
df = pd.DataFrame(data=d)
results.append(df) # append this loop's df to your list of dataframes
except:
pass
final_df = pd.concat(results)

TA貢獻1873條經(jīng)驗 獲得超9個贊
您需要在循環(huán)之外創(chuàng)建一個“主”數(shù)據(jù)框。
d1 = {}
d = {}
df = pd.DataFrame(data=d1)
for i in range(4000):
try:
shape_json = json.loads(region_shape[i])
file_name = file_name_nuclei[i]
x_val = shape_json["x"]
y_val = shape_json["y"]
width_val = shape_json["width"]
height_val = shape_json["height"]
path = '/home/values/' + str(file_name)
x1 = x_val
y1 = y_val
x2 = x_val + width_val
y2 = y_val + height_val
d = {'col1': [path], 'col2': [x1], 'col3': [y1], 'col4': [x2], 'col5': [y2], 'col5': ['nucleus']}
df2 = pd.DataFrame(data=d1)
df.update(df2)
except:
pass
并且 d1 在所有代碼中都是空的。當您嘗試使用 df2 更新 df 時,df2 也是空的。
添加回答
舉報