how to make 1 by n dataframe from series in pandas?

余生长醉 提交于 2019-12-01 04:05:10

问题


I have a huge dataframe, and I index it like so:

df.ix[<integer>]

Depending on the index, sometimes this will have only one row of values. Pandas automatically converts this to a Series, which, quite frankly, is annoying because I can't operate on it the same way I can a df.

How do I either:

1) Stop pandas from converting and keep it as a dataframe ?

OR

2) easily convert the resulting series back to a dataframe ?

pd.DataFrame(df.ix[<integer>]) does not work because it doesn't keep the original columns. It treats the <integer> as the column, and the columns as indices. Much appreciated.


回答1:


You can do df.ix[[n]] to get a one-row dataframe of row n.



来源:https://stackoverflow.com/questions/33458865/how-to-make-1-by-n-dataframe-from-series-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!