I have a data frame df 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
df=df.iloc[0]
df
like
0 0
2 2
5 5
3 3
but my output come like
0 0
1 2
2 5
3 3
CodePudding user response:
Use rename
for convert indices:
s = df.iloc[0].rename(df.iloc[0])
print (s)
0 0
2 2
5 5
3 3
Name: 0, dtype: int32
If need one column DataFrame
:
df1=df.iloc[0].rename(df.iloc[0]).to_frame('col')
print (df1)
col
0 0
2 2
5 5
3 3
Or:
s = pd.Series(df.iloc[0].to_numpy(), index=df.iloc[0])
#for oldier pandas versions
#s = pd.Series(df.iloc[0].values, index=df.iloc[0])
print (s)
0 0
2 2
5 5
3 3
dtype: int32