Reputation: 379
I'm trying to add a Series with multiindex to a dataframe, and I want to set the index of that new row to be the name of Series
stock factors dates
a 0 2019-01-01 0.0
1 2019-01-01 27.0
0 2019-01-01 7.0
1 2019-01-01 24.0
What I have tried is the code below:
series.name = 'Myindex'
df = df.append(series)
The goal is to add a new row to the existing dataframe with index set to be 'Myindex'
But I got
TypeError: object of type 'int' has no len()
Upvotes: 0
Views: 30
Reputation: 862771
I believe you need create columns in new DataFrame
by index values and then use rename
:
print (series)
stock factors dates
a 0 2019-01-01 0.0
1 2019-01-01 27.0
0 2019-01-01 7.0
1 2019-01-01 24.0
dtype: float64
df = pd.DataFrame(columns=series.index)
df = df.append(series.rename('Myindex'))
print (df)
stock a
factors 0 1 0 1
dates 2019-01-01 2019-01-01 2019-01-01 2019-01-01
Myindex 0.0 27.0 7.0 24.0
Upvotes: 1