简体   繁体   中英

How can I append a pandas Series to a DataFrame?

I realize this question has been asked before, but none of those seem to work for me. I have a series called state that looks like:

cash          17378.787327
num_shares      895.000000
cost_basis      102.153149
open_price      106.300003
close_10        108.889999
close_9         109.790001
close_8         109.209999
close_7         108.230003
close_6         109.330002
close_5         108.080002
close_4         106.910004
close_3         106.419998
close_2         104.410004
close_1         106.650002
dtype: float64

I want to append it to a DataFrame , so I have:

X = pd.DataFrame()
X.append(state)

But this gives an error TypeError: Can only append a Series if ignore_index=True or if the Series has a name , so I try:

X.append(state, ignore_index=True)

This doesn't actually append anything to X :

Empty DataFrame
Columns: []
Index: []

What am I doing incorrectly?

你应该可以只做 X['state'] = state

这就是我最终做的:

X = X.append([state], ignore_index=True)

不止一列是一个数据框,所以你可以试试这个:

X=pd.concat([X,state], axis=1)


Using

state.name=0
X = pd.DataFrame()
X.append(state)

Looks like you want both the labels and the values to be appended to the dataframe. You need to reset the Series index

state.reset_index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM