I have the following data frame:
import pandas as pd
d = {'gene' : ['foo','bar'],'score' : [4., 3.,]}
df = pd.DataFrame(d)
df.set_index('gene',inplace=True)
Which make:
In [56]: df
Out[56]:
score
gene
foo 4
bar 3
In [58]: type(df)
Out[58]: pandas.core.frame.DataFrame
What I want to do is to turn it into a Series. I expect it to to return:
gene
foo 4
bar 3
#pandas.core.series.Series
I tried this but it doesn't work:
In [64]: type(df.iloc[0:,])
Out[64]: pandas.core.frame.DataFrame
In [65]: df.iloc[0:,]
Out[65]:
score
gene
foo 4
bar 3
What's the right way to do it?
s = df.squeeze()
>>> s
gene
foo 4
bar 3
Name: score, dtype: float64
To get it back to a dataframe:
>>> s.to_frame()
score
gene
foo 4
bar 3
Try swapping the indices in the brackets:
df.iloc[:,0]
This should work.
Swapping the indices would solve the problem easily:
In [64]: type(df.iloc[0:,])
Out[64]: pandas.core.frame.DataFrame
In [65]: df.iloc[[:,0] // Swaped the indices
Out[65]:
score
gene
foo 4
bar 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With