繁体   English   中英

比较来自不同数据帧的两列值

[英]Compare two column values from different dataframes

我有两个使用 pandas 读取的数据帧。 两者都包含一个 Date 列和一个 Stock 列,我想知道这两个列中的相应值是否匹配。 如果它们匹配,我想使用来自 unique_values 的相应数量和价格值更新 test_version。

我正在使用 Python 和 Jupyter 笔记本。

# unique_values
       Index Stock  Date      Volume       Price   Score
0          1   ASO     1   4650600.0   31.139999  0.5719
272      273   GME     1   6218300.0  184.500000  0.9995
403      404   AMC     1  44067000.0   10.200000  0.9995
435      436  TSLA     1  28271800.0  691.619995  0.9686
509      510   AMD     1  29327900.0   81.440002  0.9686
...      ...   ...   ...         ...         ...     ...
11185  11186  AAPL    15  94812300.0  133.110001 -0.9399
11292  11293  BABA    15  12093900.0  229.880005  0.3907
11302  11303  CLOV    15  41659000.0    8.620000  0.9519
11464  11465   NIO    15  71208600.0   36.930000  0.4588
11478  11479  MVIS    15  16808800.0   10.390000  0.9753

[192 rows x 6 columns]

# test_version
   Stock Date  Volume  Price     Score
0    GME    1       1      1  0.194760
1    GME    2       1      1  0.126104
2    GME    3       1      1  0.041961
3    GME    4       1      1  0.039760
4    GME    5       1      1  0.105480
..   ...  ...     ...    ...       ...
10  CLOV   11       1      1       NaN
11  CLOV   12       1      1  0.145852
12  CLOV   13       1      1  0.224382
13  CLOV   14       1      1  0.226059
14  CLOV   15       1      1  0.120781

[210 rows x 5 columns]

我不确定我是否正确解决了这个问题,但这是我尝试过的:

unique_volume.reset_index(drop=True)
test_version.reset_index(drop=True)

test_version['Volume'] = np.where(test_version['Date'] == unique_volume['Date'] and test_version['Stock'] == unique_volume['Stock'], unique_volume['Volume'])


#Output
ValueError: Can only compare identically-labeled Series objects

我渴望以如下形式获得 output:

# Desired Output
   Stock Date  Volume  Price     Score
0    GME    1   6218300.0  184.500000  0.194760
..   ...  ...     ...    ...       ...

14  CLOV   15   6218300.0  184.500000  0.120781

[210 rows x 5 columns]

如果我正确理解了您的问题,合并( pd.merge: left join )DataFrames 应该适合您:

test_version = pd.merge(test_version[['Date', 'Stock']], unique_volume[['Date', 'Stock', 'Volume', 'Price']], on = ['Date', 'Stock'], how = 'left')

暂无
暂无

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

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