Skip to content
Advertisement

Difference of letting DataFrame’s column

I don’t know the difference of two ways that I let columns of DataFrame. the codes are here:

# let X be any 2-d array.
A=pd.DataFrame(data=X, columns=['ftr1', 'ftr2'])
A['ftr3']=1
print(A['ftr3'])

B=pd.DataFrame(data=X, columns=[['ftr1', 'ftr2']])
B['ftr3']=1
print(B['ftr3'])

when I printed A[‘ftr3’] to see elements of ftr3 of A, there was no problem.

But when I printed B[‘ftr3’], the problem occured:

TypeError: only integer scalar arrays can be converted to a scalar index

Moreover, the reason I’m confused with this result was that print(A) and print(B) prints exactly same results.

A=pd.DataFrame(data=X, columns=['ftr1', 'ftr2'])
A['ftr3']=1
print(A)
B=pd.DataFrame(data=X, columns=[['ftr1', 'ftr2']])
B['ftr3']=1
print(B)

the results are here:

print(A) print(A)

print(B) print(B)

I don’t get the points..plz help and thank for your reply.

Advertisement

Answer

Use print(B[['ftr3']]) instead of print(B['ftr3'])

The architecture of the columns is not the same : use A.columns and B.columns to compare them.

A.columns will return : Index(['ftr1', 'ftr2', 'ftr3'], dtype='object')
B.columns will return : MultiIndex([('ftr1',),('ftr2',),('ftr3',)],)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement