使用两个 Pandas 数据框创建列表列表

我有这两个数据框:1)这里的数据按 station_id(从 1 到 98)和时间(从 27-01-2020 到 26-05-2020 每小时的数据)分组

http://img3.mukewang.com/64084d2a0001796506240739.jpg

  1. 在第二个数据框中,我有每个 station_id 的纬度和经度值。

http://img.mukewang.com/64084d3a000198bf06560721.jpg

我的目标是以这种格式创建一个列表列表:


     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], ...]


为了让第一个数据框中的每一行都有一个列表 [latitude, longitude, flow, month, day]。我尝试使用以下代码:


import pandas as pd

import datetime as dt


df = pd.read_csv("readings_by_hour.csv")

df['time'] = pd.to_datetime(df['time'])

df1 = pd.read_csv("stations_info.csv")


i = 0

a = []

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 += 1


print(b)


但它似乎不起作用,尽管它没有给出任何错误,但确实没有给出任何输出。


翻翻过去那场雪
浏览 99回答 2
2回答

扬帆大鱼

您可以合并列上的两个数据框station_id,然后像这样创建列表列表:merged_df = pd.merge(df, df1, left_on = 'station_id', right_on = 'station_id')list_of_lists =[]&nbsp;&nbsp;&nbsp;# Iterate over each row&nbsp;for index, row in merged_df.iterrows():&nbsp; &nbsp; # Create list for the current row&nbsp;&nbsp; &nbsp; rowlist =[row.latitude, row.longitude, row.flow, row.hour, row.month, row.day]&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; # append the list to the final list&nbsp;&nbsp; &nbsp; list_of_lists.append(rowlist)&nbsp;您可以使用该模块从列datetime中提取月、日、小时Datepd.merge有关更多信息,请参阅 pandas 文档: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

倚天杖

在给定的代码中,您试图将 print 函数的返回值分配给a,然后将其添加到b. a在这里,的值为null。因此,当您尝试打印该值时,您将得到空字符串。我已经进行了更正以使其有效。希望能帮助到你..while i < count:&nbsp; &nbsp; if df['station_id'][i] == df1['station_id'][i]:&nbsp; &nbsp; &nbsp; &nbsp; a = [df1['latitude'][i],df1['longitude'][i], df['flow'][i], df['time'][i].hour,df['time'][i].month,df['time'][i].day]&nbsp; &nbsp; &nbsp; &nbsp; b.append(a)&nbsp; &nbsp; &nbsp; &nbsp; i += 1print(b)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python