DEFINING A DATAFRAME OBJECT
Pandas Dataframe
A pandas dataframe is an extension of the Panda Series to have n-dimensions. This allows for a robust tabular structure of acccessing data. Much like a matrix, we can apply transformations to a dataframe as we would on a matrix.Below is an example of defining a dataframe object.
Defining a Dataframe Object
import pandasdf = pd.DataFrame( {'Country': ['USA', 'Canada', 'Mexico'],
'Population': [325, 36, 127],
'Year': [2018, 2017, 2017]})
df as pd
df = pd.DataFrame( {'Country': ['USA', 'Canada', 'Mexico'],
'Population': [325, 36, 127],
'Year': [2018, 2017, 2017]})
df
Country | Population | Year | |
---|---|---|---|
0 | USA | 325 | 2018 |
1 | Canada | 36 | 2017 |
2 | Mexico | 127 | 2017 |
We see above the tabular structure of the dataframe. let’s change the index
Changing Index
df.set_index('Country', inplace=True)
df
Population | Year | |
---|---|---|
Country | ||
USA | 325 | 2018 |
Canada | 36 | 2017 |
Mexico | 127 | 2017 |
What is the use of inplace in pandas?
When inplace = True is used, it performs operation on data and nothing is returned. When inplace=False is used, it performs operation on data and returns a new copy of data.