第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

使用兩個(gè) Pandas 數(shù)據(jù)框創(chuàng)建列表列表

使用兩個(gè) Pandas 數(shù)據(jù)框創(chuàng)建列表列表

翻翻過去那場雪 2023-03-08 16:34:30
我有這兩個(gè)數(shù)據(jù)框:1)這里的數(shù)據(jù)按 station_id(從 1 到 98)和時(shí)間(從 27-01-2020 到 26-05-2020 每小時(shí)的數(shù)據(jù))分組在第二個(gè)數(shù)據(jù)框中,我有每個(gè) station_id 的緯度和經(jīng)度值。我的目標(biāo)是以這種格式創(chuàng)建一個(gè)列表列表:     latitude           longitude      flow   hour  month  day[[53.37947845458979, -1.46990168094635, 278.0, 0.0, 1.0, 27.0],  [53.379791259765604, -1.46999669075012, 122.0, 0.0, 1.0, 27.0],  [53.380035400390604, -1.47001004219055, 58.0, 0.0, 1.0, 27.0], ...]為了讓第一個(gè)數(shù)據(jù)框中的每一行都有一個(gè)列表 [latitude, longitude, flow, month, day]。我嘗試使用以下代碼:import pandas as pdimport datetime as dtdf = pd.read_csv("readings_by_hour.csv")df['time'] = pd.to_datetime(df['time'])df1 = pd.read_csv("stations_info.csv")i = 0a = []b = []count = df1['station_id'].count()while i < count:    if df['station_id'][i] == df1['station_id'][i]:        a = print(df1['latitude'][i] + ", " + df1['longitude'][i] + ", " + df['flow'][i] + ", " + df['time'].dt.hour + ", " + df['time'].dt.month + ", " + df['time'].dt.day)        b += [a]        i += 1print(b)但它似乎不起作用,盡管它沒有給出任何錯(cuò)誤,但確實(shí)沒有給出任何輸出。
查看完整描述

2 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以合并列上的兩個(gè)數(shù)據(jù)框station_id,然后像這樣創(chuàng)建列表列表:


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中提取月、日、小時(shí)Date


pd.merge有關(guān)更多信息,請參閱 pandas 文檔: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html


查看完整回答
反對 回復(fù) 2023-03-08
?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

在給定的代碼中,您試圖將 print 函數(shù)的返回值分配給a,然后將其添加到b. a在這里,的值為null。因此,當(dāng)您嘗試打印該值時(shí),您將得到空字符串。


我已經(jīng)進(jìn)行了更正以使其有效。希望能幫助到你..


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)


查看完整回答
反對 回復(fù) 2023-03-08
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號