简体   繁体   English

如何根据条件将来自两个单独列的数据连接到另一个 dataframe?

[英]How to join data from two seperate columns based upon condition to another dataframe?

I want to add data from df2 if date is greater than 01/01/2015 and df1 if its below than 01/01/2015.如果日期大于 2015 年 1 月 1 日,我想从 df2 添加数据,如果日期小于 2015 年 1 月 1 日,我想从 df1 添加数据。 Unsure how to do this as the columns are of difference length.不确定如何执行此操作,因为列的长度不同。

I have a main DF df1, and two dataframes containing data called df2 and df3.我有一个主 DF df1,以及两个包含名为 df2 和 df3 的数据的数据帧。

df2 looks something like this: df2 看起来像这样:

day                 Value
01/01/2015           5
02/01/2015           6
...

going up to today,一直到今天,

I also have DF3 which is the same data but goes from 2000 to today so like我也有 DF3,它是相同的数据,但从 2000 年到今天,就像

day                 Value
01/01/2000           10
02/01/2000           15
...

I want to append a Value column to DF1 that is the values of DF3 when date is less than 2015, and the values of DF2 when the date is more than 01/01/2015( including).我想 append 到 DF1 的值列,即日期小于 2015 年时 DF3 的值,以及日期超过 2015 年 1 月 1 日(包括)时的 DF2 值。 Unsure how to do this using a condition.不确定如何使用条件执行此操作。 I think there is a way with np.where but unsure how.我认为 np.where 有一种方法,但不确定如何。

To add more context.添加更多上下文。

I want to codify this statement:我想编纂这个声明:

df1[values] = DF2[Values] when date is bigger than 1/1/2015 and df1[values] = DF3[Values] when date is less than 1/1/2015 df1[values] = DF2[Values] 当日期大于 1/1/2015 和 df1[values] = DF3[Values] 当日期小于 1/1/2015

If you want to join 2 dataframe, you can use df1.mrege (df2,on="col_name").如果要加入2 dataframe,可以使用df1.mrege(df2,on="col_name")。 If you want a condition on one dataframe and join with the other,如果您想要一个 dataframe 的条件并与另一个加入,

do it like this,像这样做,

import pandas as pd
#generating 2 dataframes
date1 = pd.date_range(start='1/01/2015', end='2/02/2015')
val1 = [2,3,5]*11. #length equal to date
dict1 = {"Date":date2,"Value":val1}
df1 = pd.DataFrame(dict1)
df1. #view df1
date2 = pd.date_range(start='1/01/2000', end='2/02/2020')
val2 = [21,15]*3669. #length equal to date
dict2 = {"Date":date2,"Value":val2}
df2 = pd.DataFrame(dict2)
df2. #view df1
df3 =df1[df1["Date"]<"2015-02-01"] #whatever condition you want apply
and store on different dataframe
df2.merge(df3,on="Date")
df2 #joined dataframe

This is how you can join 2 dataframe on date with a condition, just apply the conditon and store i another dataframe and join it with the first dataframe by df.merge(df3,on="Date")这就是您如何在有条件的日期加入 2 个 dataframe,只需应用条件并存储另一个 dataframe 并将其与第一个 Z6A8064B5DF479455500="53C47C5507DZ" 加入。

暂无
暂无

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

相关问题 Pandas:如何根据 2 个单独数据帧的两列之间的条件进行连接 - Pandas: how to make a join based on a condition between two columns of 2 seperate dataframes 根据来自Pandas Python中另外两个单独数据框的列名创建列值 - Create columns values based on columns names from another two seperate dataframe in Pandas Python 如何根据列条件将选定的列从数据框中复制到另一个 - How to copy selected columns from a dataframe to another based on a column condition 如何将两列连接到Pandas的另一个独立列中? - How do a join two columns into another seperate column in Pandas? 根据另一个数据帧的列值的条件将数据添加到数据帧中的列 - Adding data to columns in a dataframe based on condition on column values of another dataframe 如何根据条件交叉引用数据框中的列? - How to crosstab columns from dataframe based on a condition? 如何基于 2 列从另一个 dataframe 获取数据 - How to take data from another dataframe based on 2 columns 如何根据其他列的值从熊猫数据框中提取数据? - How to extract data from a pandas dataframe based upon values of other columns? 如何根据每个 dataframe 中两个不同日期列的日期条件合并两个数据框? - How to merge two dataframes based on a date condition from two different date columns in each dataframe? 将具有 df 的两列值的两个数据框与另一个数据框的单列值连接起来。 基于某些条件? - Join two data frame with two columns values of a df with a single column values of another dataframe. based on some conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM