简体   繁体   English

比较两个数据框以获得另一个数据框中的比较值

[英]Compare two dataframes to get comparison value in in another dataframe

I have two dataframes, df1 and df2 , each in the following format with the same index and different values: 我有两个数据df1df2 ,每个数据df2都采用以下格式,具有相同的索引和不同的值:

       Value
Date
01-01    60
01-02    70
01-03   -80 

I need to compare the two dataframes where values of df1 < df2 and get only those values for which the comparison stands true with their respective indices in a third dataframe df_new . 我需要比较df1 < df2值的两个数据帧,并仅获取比较正确的值以及它们在第三个数据帧df_new的相应索引。

I would suggest that you merge your two data frames based on the index so that you can compare the values between each columns. 我建议您根据索引合并两个数据框,以便可以比较各列之间的值。

Try this: 尝试这个:

import pandas

df1 = pandas.DataFrame(
  data=[60, 70, -80],
  index=['01-01', '01-02', '01-03'],
  columns=['Value'])

df2 = pandas.DataFrame(
  data=[59, 69, -79],
  index=['01-01', '01-02', '01-03'],
  columns=['Value'])


df3 = df1.merge(df2, how='outer', left_index=True, right_index=True, suffixes=('_1', '_2'))

df3['Delta'] = df3['Value_2'] - df3['Value_1']

It will return you the following dataframe: 它将返回以下数据框:

       Value_1  Value_2  Delta
01-01       60       59     -1
01-02       70       69     -1
01-03      -80      -79      1

Here is the link to the merge method: pandas.DataFrame.merge 这是合并方法的链接: pandas.DataFrame.merge

暂无
暂无

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

相关问题 比较两个数据帧并获得最接近的匹配 dataframe - compare two dataframes and get nearest matching dataframe 比较两个数据帧并在 Python 中创建比较矩阵? - Compare two dataframes and create comparison matrix in Python? Pandas:比较两个单独的数据帧,找到一列的交集,但使用另一列中的值来确定要保留哪个数据帧? - Pandas: compare two separate dataframes, find intersection of one column, but use value in another column to determine which dataframe to keep? 使用两个数据框如何比较查找值作为 substring 在另一个 dataframe 的列中创建一个新列,如果匹配存在 - Using two dataframes how can I compare a lookup value as a substring in the column in another dataframe to create a new column if the match exists 比较两个数据帧并创建另一个 - compare two dataframes and create another 比较来自两个数据帧的值 - Compare value from two dataframes 在 Pandas 中加入两个数据帧,从另一个 dataframe 中删除值 - Joining two dataframes in Pandas remove value from another dataframe 我有两个数据框。 我想将一个数据框的标题与另一数据框的一列的内容进行比较 - I have two dataframes. I wanted to compare header of one dataframe with the content of one column in another dataframe Python dataframe - 比较两个数据帧之间的行 - Python dataframe - compare rows between two dataframes 比较两个数据框的列并创建一个新的数据框 - Compare columns of two dataframes and create a new dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM