2 回答

TA貢獻1799條經驗 獲得超9個贊
您可以合并列上的兩個數據框station_id,然后像這樣創建列表列表:
merged_df = pd.merge(df, df1, left_on = 'station_id', right_on = 'station_id')
list_of_lists =[]
# Iterate over each row
for index, row in merged_df.iterrows():
# Create list for the current row
rowlist =[row.latitude, row.longitude, row.flow, row.hour, row.month, row.day]
# append the list to the final list
list_of_lists.append(rowlist)
您可以使用該模塊從列datetime中提取月、日、小時Date
pd.merge有關更多信息,請參閱 pandas 文檔: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

TA貢獻1828條經驗 獲得超3個贊
在給定的代碼中,您試圖將 print 函數的返回值分配給a,然后將其添加到b. a在這里,的值為null。因此,當您嘗試打印該值時,您將得到空字符串。
我已經進行了更正以使其有效。希望能幫助到你..
while i < count:
if df['station_id'][i] == df1['station_id'][i]:
a = [df1['latitude'][i],df1['longitude'][i], df['flow'][i], df['time'][i].hour,df['time'][i].month,df['time'][i].day]
b.append(a)
i += 1
print(b)
添加回答
舉報