How to create a partial dataframe from another dataframe with a different index

Viewed 429

In python 3.9, I have declared the following python DataFrame, raw

>>> print(raw)

            Date      AAPL.O   MSFT.O  INTC.O   AMZN.O    GS.N     SPY  \

1     2010-01-04   30.572827   30.950   20.88   133.90  173.08  113.33 

...

from which I would like to extract one column (e.g. AAPL.O) and the corresponding Date (as index) I thought that a possible way would be

raw2=pandas.DataFrame(data=raw['AAPL.O'], index=raw['Date'])

This doesn't return any error, however, when I print it, I get:

Date         AAPL.O   
2010-01-04   NaN
2010-01-05   NaN
...

If I remove index=raw['Date'] from the declaration

raw2=pandas.DataFrame(data=raw['AAPL.O']) 

it works as expected (except for the fact that I don't have the index that I wanted).

So I don't understand why but passing a column of the former dataframe (as data) and another column as index, brings me these unexpected NaN.

While there are better ways to achieve my goal, I expected this one to work as well. So probably I am missing something more fundamental which I would like to understand

1 Answers

Well, the index is not part of the data, so it might be part of the problem. Even so, I don't really know how it expects to handle the index parameter. It should work with a df.index.

Anyway, what you can do

raw2=pandas.DataFrame(data=raw[['AAPL.O','Date']])
raw2.set_index('Date')

In this case, I have chosen the data from both columns. In fact, I might just do raw[['AAPL.O','Date']].copy().

Related