Reputation: 71
I'm trying to add a column from a pandas dataframe to another pandas dataframe using date columns to match them up. As you can see, one dataframe has one extra date I'd like to just skip. I figured an if statement would work for this but I'm getting: ValueError: Can only compare identically-labeled Series objects
Date Adj Close
0 2020-01-02 9.903333
1 2020-01-03 9.883333
2 2020-01-06 9.883333
3 2020-01-07 9.883333
4 2020-01-08 9.913333
.. ... ...
163 2020-08-25 10.133333
164 2020-08-26 10.173333
165 2020-08-27 10.183333
166 2020-08-28 10.206667
167 2020-08-31 10.203334
[168 rows x 2 columns]
Date Capital Stock
0 2020-01-02 7251.39
1 2020-01-03 47200.86
2 2020-01-06 119020.28
3 2020-01-07 11751250.39
4 2020-01-08 4790267.25
.. ... ...
162 2020-08-25 6348.29
163 2020-08-26 -11870.05
164 2020-08-27 73210.22
165 2020-08-28 120581.32
166 2020-08-31 134085.86
[167 rows x 2 columns]
here's what I've got!
if dshares_df['Date'] == price3['Date']:
dshares_df['close'] = price3['Adj Close']
Upvotes: 0
Views: 22
Reputation: 357
For this case you want to merge the two dataframes,
Try:
merged_df = dshares_df.merge(price3, on='Date')
# To filter out the extra row
merged_df = merged_df.dropna()
see the docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html
Upvotes: 1