Sams Got the blues
Sams Got the blues

Reputation: 3

What would cause .loc[] to return inconsistent shape?

I have a dataframe whoes shape is

print(trainData.shape)

(4146, 17)

What would be the reason for

c =  trainData.loc[0,:]
print(c.shape)

returning

(17,)

I need the output to be a dataframe and not series

[[0]] returns (1,17)

Upvotes: 0

Views: 43

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30032

If you want to select first row as dataframe, you can use

c =  trainData.loc[[0],:]
# or
c =  trainData.loc[0,:].to_frame().T

If you want to select first column as dataframe,

c =  trainData.iloc[:,[0]]
# or
c =  trainData.iloc[:,0].to_frame()

Upvotes: 1

Related Questions