Home > Software engineering >  How to trim columns with NaN while concatenating two series
How to trim columns with NaN while concatenating two series

Time:12-31

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.

CodePudding user response:

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

CodePudding user response:

There are many posts answering questions around that topic so this might be a duplicate. For instance this post answers a very similar question. firs you need to assign the result of the concat() process to a variable, say df and then you simply use dropna(). Make sure to set the parameter inplace to False or to reassign the result of the function call 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
  • Related