I frequently go back and forth between using dataframe object attributes to refer to columns as well as using the bracket method.
I am wondering which format is considered "best practice," and if there are any performance differences between the two (or could this potentially vary based upon the circumstance?). I am not finding many resources on this subject.
Here's a simplistic example of what I mean: creating the column "green," with rows being True if columns "blue" and "yellow" are True, otherwise the rows are false.
# using brackets.
df['green'] = np.where((df['blue']==True) & (df['yellow']==True), True, False)
vs.
# using periods.
df['green'] = np.where((df.blue==True) & (df.yellow == True), True, False)
I often find myself using the latter as it looks cleaner, is shorter, and is easier to type. However, I often see pandas examples here and other sources using both methods.
- Is there a performance difference in using either format?
- Which format is considered best practice?