-1

What is the difference of the below codes?

When I executed python codes, I got different results.

1st code is easy for me, but 2nd code is what I want.

I want to know the reason.

  1. variable.columns.isnull() eg. boat_data.gear.isnull()
  2. variable[variable.columns.isnull()] eg. boat_data[boat_data.gear.isnull()]
jps
  • 20,041
  • 15
  • 75
  • 79
  • Please use code blocks for code. Additionally, provide a minimal working example (with a tiny filled variable). In this case, it is unclear what the type is of `variable` and as such it is hard to answer your question. – Kroshtan Jan 13 '22 at 08:52

2 Answers2

0

Working under the assumption that variable, and thus boat_data is a Pandas DataFrame, the difference is that option 2 uses the truth values from the isnull() call to make a subset of the dataframe, selecting all rows for which gear.isnull() is true.

Kroshtan
  • 637
  • 5
  • 17
-1
variable.columnname.isnull()

returns a series of truth values for the columnname.isnull() condition in each row of the dataframe.

Putting that inside brackets filters the dataframe to just the rows where the condition is true.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    `.columns` is a misuse as it's an attribute when `variable` is a DataFrame. –  Jan 13 '22 at 09:02
  • I'm not a pandas expert, but I thought it allowed you to use either `df.columnname` or `df['columnname']` – Barmar Jan 13 '22 at 09:07
  • yes, but `df.columns` is special. Like `df.shape` is. Not my downvote btw. –  Jan 13 '22 at 09:08
  • it's not literal, it's a placeholder for an actual column name. He gave a specific example of `gear`. – Barmar Jan 13 '22 at 09:09
  • 1
    yeah I was writing for possible future readers, kind of –  Jan 13 '22 at 09:10