Reputation: 432
I have two dataframes which I've read with pandas. Both contains a Date column and a Stock column, and I want to find out if the corresponding values in those two columns are matching. If they match I would like to update test_version with the corresponding Volume & Price values from unique_values.
I am using Python and Jupyter notebook.
# 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]
I am uncertain if I've approached the problem correctly, but here's what I've tried:
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
I'm aspiring to get an output in the form of something like this:
# 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]
Upvotes: 1
Views: 369
Reputation: 2061
If I understood your question correctly, merging (pd.merge : left join)the DataFrames should work for you:
test_version = pd.merge(test_version[['Date', 'Stock']], unique_volume[['Date', 'Stock', 'Volume', 'Price']], on = ['Date', 'Stock'], how = 'left')
Upvotes: 1