Home > Software engineering >  What is the meaning of `df[df['factor']]` syntax in Pandas?
What is the meaning of `df[df['factor']]` syntax in Pandas?

Time:01-27

I am reading some Pandas code where they have this bit of code

df_new = df[df['factor']]

I understand that df['factor'] gets the column called factor from df. But what does the second df do?

CodePudding user response:

Assumed your have df like below

df
  factor  a  b  c  d
0      a  1  1  1  1
1      b  1  1  1  1
2      c  1  1  1  1

df[df['factor']] #select the column which contain in the factor columns
   a  b  c
0  1  1  1
1  1  1  1
2  1  1  1

If your factor is True and False

df[df['factor']]
   factor  a  b  c  d
0    True  1  1  1  1


#   factor  a  b  c  d
#0    True  1  1  1  1
#1   False  1  1  1  1
#2   False  1  1  1  1
  •  Tags:  
  • Related