问题
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