Suppose that you have a pandas DataFrame
named df
with columns ['a','b','c','d','e']
and you want to create a new DataFrame
newdf
with columns 'b'
and 'd'
. There are two possible ways to do this:
newdf = df[['b','d']]
or
newdf = df.loc[:,['b','d']]
The first is using the indexing operator. The second is using .loc
. Is there a reason to prefer one over the other?