Last answered:

28 Dec 2023

Posted on:

11 May 2023

0

Difference between Similar Expressions

Could someone please explain when to use either one of below expressions?

- rw['date']

- rw.date

- 'date'

1 answers ( 0 marked as helpful)
Posted on:

28 Dec 2023

1

I am not an expert, but what I have understood based on my usage experience is as follows...


The actual syntax is rw['date'] and rw.date is just a syntactic sugaring for convenience. However, if our field name in the data frame has spaces, for example, 'Sales Volume', then we will have to use rw['Sales Volume'] and we can't use rw.Sales Volume . So if we have a habit of naming the columns using a cabab case, then the shorter and conventint rw.date kind of syntax would be more useful. But these two usages apply when we have to specify both the dataframe name and the field name. 


The other syntax of using just 'date' is applicable or allowed in scenarios where we don't have to explicitly state the dataframe name and based on context, the dataframe name will be implicitly determined. For example, rw.set_index('date'). In this case we don't have to say rw.set_index(rw['date']) or rw.set_index(rw.date)

Submit an answer