简体   繁体   English

合并/加入Pandas数据框

[英]Combine/Join Pandas Data Frames

I have two data frames, the first on is from a scooter renting company in an island it includes data about the scooter type, duration, starting and ending time and many others fields, the second one includes meteorological data from this island. 我有两个数据框,第一个是来自岛上的摩托车租赁公司,它包括有关踏板车类型,持续时间,开始和结束时间以及许多其他领域的数据,第二个包括来自该岛的气象数据。

Because the meteorological data for each day have 8 records/measures every 3 hours (eg at 01.00am , at 04:00am till 22:00pm) I transformed/cut the starting time in the first data frame to 8 bins in order to combine for each record the corresponding weather conditions. 因为每天的气象数据每3小时有8个记录/措施(例如在凌晨01.00,凌晨04:00到晚上22:00),我将第一个数据框中的开始时间转换/切割为8个分区,以便合并为每个记录相应的天气状况。

how can I combine these two frames in order for each rent to have the corresponding weather conditions? 我如何组合这两个框架,以便每个租金都有相应的天气条件?

Suppose that I have the fields df1.rent_id, df1.rent_day, df1.starting hour(from 1 to 8) and df2.day, df2.hour(from 1 to 8), df2.temp and so on 假设我有字段df1.rent_id,df1.rent_day,df1.starting小时(从1到8)和df2.day,df2.hour(从1到8),df2.temp等等

How can combine/join these two dataframe to get what I want? 如何组合/加入这两个数据帧以获得我想要的东西?

As it sees, each record in the second data frame have many records in the first but each record in the first data frame relates to only one record to the second. 如它所见,第二个数据帧中的每个记录在第一个数据帧中有许多记录,但第一个数据帧中的每个记录仅与第二个数据帧中的一个记录相关。

I have confused with the join types (inner, outer, left, right) what I need? 我对连接类型(内部,外部,左侧,右侧)的困惑与我需要的是什么?

Thanks 谢谢

First you need to set a reference column to sort the merged dataframe eg you can use starting time for df1 and time for df2 首先,您需要设置一个参考列来对合并的数据帧进行排序,例如,您可以使用df1 starting timedf2 time

df1['reference_time']=df1['starting time']
df2['reference_time']=df2.['time']

Then set index and join them according to the order of reference time 然后设置索引并根据参考时间的顺序将它们连接起来

df1.set_index('reference time').join(df2.set_index('reference_time'))

as for join mode option, you can have a look at the documentation here 对于加入模式选项,您可以在此处查看文档

how : {'left', 'right', 'outer', 'inner'}, default 'left' How to handle the operation of the two objects. 如何:{'left','right','outer','inner'},默认'left'如何处理两个对象的操作。

left: use calling frame's index (or column if on is specified) right: use other's index. left:使用调用框架的索引(如果指定了on,则使用列)right:使用other的索引。 outer: form union of calling frame's index (or column if on is specified) with other's index, and sort it. outer:调用框架索引的形式联合(如果指定了on,则为列)与其他索引的形式联合,并对其进行排序。 lexicographically. 字典顺序。 inner: form intersection of calling frame's index (or column if on is specified) with other's index, preserving the order of the calling's one. inner:调用框架的索引(或指定的列,如果指定)与其他索引的形式交集,保留调用的索引的顺序。

The default mode is left 保留默认模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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