0
$\begingroup$

I'm learning data science in datacamp and a doubt came to my mind: if we have a dataframe cars structured like

     cars_per_cap        country  drives_right
US            809  United States          True
AUS           731      Australia         False
JPN           588          Japan         False
IN             18          India         False
RU            200         Russia          True
MOR            70        Morocco          True
EG             45          Egypt          True

then I can filter only the cars that have drives_right = True by typing print(cars[cars['drives_right']]). This will return the following:

     cars_per_cap        country  drives_right
US            809  United States          True
RU            200         Russia          True
MOR            70        Morocco          True
EG             45          Egypt          True

and that is because cars['drives_right'] is a series. I got to wonder what would happen if I used the data frame cars[['drives_right']] instead and I got the following:

     cars_per_cap country drives_right
US            NaN     NaN         True
AUS           NaN     NaN          NaN
JPN           NaN     NaN          NaN
IN            NaN     NaN          NaN
RU            NaN     NaN         True
MOR           NaN     NaN         True
EG            NaN     NaN         True

does any of you know why that happens?

$\endgroup$

1 Answer 1

0
$\begingroup$

let me explain through this

import pandas as pd
data = [{'cars_per_cap': 89, 'country': 'United States', 'drives_right': True },{'cars_per_cap': 289, 'country': 'Australia', 'drives_right': False },{'cars_per_cap': 189, 'country': 'Japan', 'drives_right': False } ]
cars =pd.DataFrame(data, index=["US", "AUS", "JPN"])
print (cars)

output :

     cars_per_cap        country  drives_right
US             89  United States          True
AUS           289      Australia         False
JPN           189          Japan         False

You can see the index created here are 'US', 'AUS', 'JPN'

#With three  column indices, last values same as dictionary key , others not 
df1 = pd.DataFrame(data, index=['US', 'AUS', 'JPN'], columns=['a', 'b', 'drives_right'])
print (df1)

output :

      a   b  drives_right
US  NaN NaN          True
AUS NaN NaN         False
JPN NaN NaN         False

Please see , df1 DataFrame is created with some column index other than the dictionary key; thus, appended the NaN’s in place for the values.

Same reason , when you use :

print(cars[cars['drives_right']])

  cars_per_cap        country  drives_right
US            89  United States          True

when you use :

print (cars[cars[['drives_right']]])

     cars_per_cap country drives_right
US            NaN     NaN         True
AUS           NaN     NaN          NaN
JPN           NaN     NaN          NaN
$\endgroup$
1
  • $\begingroup$ I don't understand why one thing implicates the other $\endgroup$ Commented Mar 24, 2022 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.