I am trying to reverse values of DataFrame in Python as in the following codes. Strangely, x retuns what expected, but not y.
import pandas as pd
import pandas_datareader as pdr
x = pdr.DataReader('TQQQ', data_source='yahoo', start='2000-01-01')
y = pdr.DataReader('^NDX', data_source='yahoo', start='2000-01-01') # ^NDX is a symbol for Nasdaq 100 Index
x[:] = x[::-1]
y[:] = y[::-1]
print(x)
print(y)
I suppose which symbol is called should not be a matter, but it seems to be matter.
Please help with it. Thank in advance for any comment and advice.
I feel there's inconsistency in the data downloaded. If you change all the columns as float type or string type, the 2 dataframes have the same behavior (doing nothing), as expected.
import pandas_datareader as pdr
x = pdr.DataReader('TQQQ', data_source='yahoo', start='2000-01-01')
print(x)
for col in x.columns:
x[col] = x[col].astype('float')
#x[col] = x[col].astype('str')
x[:] = x[::-1]
print(x)
y = pdr.DataReader('^NDX', data_source='yahoo', start='2000-01-01') # ^NDX is a symbol for Nasdaq 100 Index
print(y)
for col in x.columns:
y[col] = y[col].astype('float')
#y[col] = y[col].astype('str')
y[:] = y[::-1]
print(y)
I have tried this line df[:] = df[::-1]
on other dataframe, and it does not change anything.
However, this line df = df[::-1]
will reverse the dataframe.
You should be using these lines instead:
x = x[::-1]
y = y[::-1]
Alternatively, you could use sort_values()
to reverse the dataframe:
x = x.sort_values('Date', ascending=False)
y = y.sort_values('Date', ascending=False)
If you would like to reverse the dataframe without reversing the index, consider using
df = df.loc[::-1].set_index(df.index)