Reputation: 101
df= get_returns("SP500")
df1=df["Close"]
df2 = get_returns("NASDAQ")
df2 =df["Close"]
asset = pd.concat([df1,df2],axis=1)
asset.rename({"Close": "Close SP00","Close": "Close NASDAQ"})
print(asset)
i also tried it with
asset.rename(columns={"Close": ""Chg_Close SP00","Close": "%Chfg_Close NASDAQ"})
and with
df1.columns("")
df2.columns("")
what is suspicious is that if i print just one of the datasets than there is no column name. But i do not why, because bfore python knew what was df2 = get_returns("NASDAQ") & df2 =df["Close"]
Date
2021-03-12 NaN
2021-03-11 0.016169
2021-03-10 -0.044696
2021-03-09 -0.014814
2021-03-08 -0.025117
...
2020-03-18 -0.028696
2020-03-17 0.221267
2020-03-16 0.016338
2020-03-13 0.263508
2020-03-12 -0.070825
if i print the last row
print(asset)
then Column is shown up each but the change of renaming both didnt work.
Close Close
Date
2021-03-12 NaN NaN
2021-03-11 0.016169 0.016169
2021-03-10 -0.044696 -0.044696
2021-03-09 -0.014814 -0.014814
2021-03-08 -0.025117 -0.025117
... ... ...
Upvotes: 0
Views: 67
Reputation: 9941
what is suspicious is that if i print just one of the datasets than there is no column name
What happens is that your df1
and df2
are pd.Series
and not pd.DataFrame
, so you don't have a column name for them (they do have a name though, if you print them you should see Name: Close
at the bottom of the output).
Probably the best option is to simply create a new DataFrame and specify the column names that you want:
asset = pd.DataFrame({
'Close SP500': df1,
'Close NASDAQ': df2,
})
P.S. And if you want to rename columns in a DataFrame, you need to assign it back to the DataFrame (df = df.replace(...)
) or add inplace=True
parameter (df.replace(..., inplace=True)
, otherwise your original DataFrame is not gonna be updated. If you have duplicate column names, however, it's best to simply set them with df.columns = [...]
, as suggested in the other answer
Upvotes: 1
Reputation: 101
def get_returns(file):
dfcolumns = pd.read_csv(file + ".csv",nrows=1)
return pd.read_csv(file + ".csv", index_col = 0, parse_dates = True,dtype={"Open": float,
"High":float,
"Low":float,
"Close":float},usecols = list(range(len(dfcolumns.columns)))).pct_change()
#example
this is the get_returns function. I had to apply
dfcolumns = pd.read_csv(file + ".csv",nrows=1)
because the program always responded with these errors
TypeError: unsupported operand type(s) for /: 'str' and 'float'
result[mask] = op(xrav[mask], yrav[mask])
and then b)
IndexError: list index out of range
Upvotes: 0