Home > OS >  Extract row from a data frame and make it a new data frame
Extract row from a data frame and make it a new data frame

Time:12-07

I have a data frame like this

import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[0,2,5,3], [3,1,4,2], [1,3,5,2], [5,1,3,4], [4,2,5,1], [2,3,5,1]]))
df

Now, I need first row of data frame and make it another data frame

like

    row
0    0
1    2
2    5
3    3 

CodePudding user response:

Use DataFrame.iloc like:

df1 = df.iloc[0].to_frame('row')

For Series:

s = df.iloc[0]

CodePudding user response:

You can also use double [[ ]] and transpose it:

>>> df.iloc[[0]].T
   0
0  0
1  2
2  5
3  3
  • Related