0 votes
1 view
in Data Science by (17.6k points)
edited by

For example, I'm studying an example like this:

train['Datetime'] = pd.to_datetime(train.Datetime,format='%d-%m-%Y %H:%M') 

If I run train['Datetime'].head() and train.Datetime.head(), the results are identical. So why use one over the other? Or why use both?

1 Answer

0 votes
by (38.2k points)
edited by

Dot notation is a shortcut and less reliable than using [' '] notation. If you have a dataframe with a column 'Date Time' then you cannot use df.DateTime.head() you should use df['Date Time'].head() because dot notation will not work if there is column headers with whitespace or special characters.

There are some advantages of using the dot notation, one is in some development environments such as Jupyter notebook.

1. Using the dot notation will help in getting the popup of all available methods that are available and can be used on a dataframe column. This is not available in Jupyter notebook when using [' '] notation. 

2. If you are used to programming languages with the dot notation framework then you could have advantage in readability of the code.

3.Use of dot notation can be used where we want quick checks like in jupyter notebooks.

train.Datetime.head()

But when we want to pass variables that may come from other sources, using [] notation is more beneficial.

train['Datetime'].head()

If you want to learn  Python for Data Science then you can watch this Python tutorial:

If you want to learn data science in-depth then enroll for best data science training.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...