Skip to content
Advertisement

Extract row from a data frame and make it a new data frame

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 

Advertisement

Answer

Use DataFrame.iloc like:

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

For Series:

s = df.iloc[0]
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement