0

I am doing so:

pandas.concat([
  yfinance.download('btc-usd',interval="1d")["Close"],
  yfinance.download('aapl',interval="1d")["Close"]
],axis=1)

And this gives me a dataframe with NaN values, like this:

             Close         Close
Date
1980-12-12    0.128348           NaN
1980-12-12    0.128348           NaN
1980-12-16    0.112723           NaN
1980-12-17    0.115513           NaN
1980-12-18    0.118862           NaN
...                ...           ...
2021-12-27  180.330002  50640.417969
2021-12-28  179.289993  47588.855469
2021-12-29  179.380005  46444.710938
2021-12-30  178.199997  47178.125000
2021-12-31         NaN  47804.882812

I need to remove all rows which contain NaN value.

Lama's
  • 21
  • 1
  • 4

2 Answers2

-1

There are many posts answering questions around that topic so this might be a duplicate. For instance this post answers a very similar question. First, you need to assign the result of the concat() call to a variable, say df. Then, you can simply use dropna(). Make sure to set the parameter inplace to False or to reassign the result to a variable so that you can keep track of the rows you're interested in.

df = pandas.concat([
  yfinance.download('btc-usd',interval="1d")["Close"],
  yfinance.download('aapl',interval="1d")["Close"]
],axis=1)
df.dropna(inplace=True)
df
    Close         Close
Date
2021-12-27  180.330002  50640.417969
2021-12-28  179.289993  47588.855469
2021-12-29  179.380005  46444.710938
2021-12-30  178.199997  47178.125000
Iqigai
  • 326
  • 3
  • 10
  • Not sure why and who downgraded my answer. If there were at least some feedback about how to improve my answer I would amend it. – Iqigai Jan 07 '22 at 14:48
-2

If you data is stored in df, df.dropna(axis="index") will drop rows that have any NaNs in them.

tnwei
  • 860
  • 7
  • 15