Data Frame set_index() and reset_index()
These methods are very useful. We can dynamically change re index and reset index with out any problem.
import pandas as pd
drinks= pd.read_csv('http://bit.ly/drinksbycountry')
print(drinks.shape) #(193, 6)
print(drinks.index) #RangeIndex(start=0, stop=193, step=1)
print(drinks.columns)
"""Index(['country', 'beer_servings', 'spirit_servings', 'wine_servings',
'total_litres_of_pure_alcohol', 'continent'],
dtype='object')
"""
drinks.set_index('continent',inplace=True)
print(drinks.index)
"""
Index(['Asia', 'Europe', 'Africa', 'Europe', 'Africa', 'North America',
'South America', 'Europe', 'Oceania', 'Europe',
...
'Africa', 'North America', 'South America', 'Asia', 'Oceania',
'South America', 'Asia', 'Asia', 'Africa', 'Africa'],
dtype='object', name='continent', length=193)
"""
print(drinks.head())
"""
country ... total_litres_of_pure_alcohol
continent ...
Asia Afghanistan ... 0.0
Europe Albania ... 4.9
Africa Algeria ... 0.7
Europe Andorra ... 12.4
Africa Angola ... 5.9
[5 rows x 5 columns]
"""
print(drinks.shape) #(193, 5)
print(drinks.loc['Europe', 'spirit_servings'])
"""
continent
Europe 132
Europe 138
....
Europe 237
Europe 126
Name: spirit_servings, dtype: int64
"""
drinks.index.name = None
print(drinks.index)
"""
Index(['Asia', 'Europe', 'Africa', 'Europe', 'Africa', 'North America',
'South America', 'Europe', 'Oceania', 'Europe',
...
'Africa', 'North America', 'South America', 'Asia', 'Oceania',
'South America', 'Asia', 'Asia', 'Africa', 'Africa'],
dtype='object', length=193)
"""
drinks.index.name='continent'
drinks.reset_index(inplace=True) # Resetting Index inplace
print(drinks.head(2))
"""
continent country ... wine_servings total_litres_of_pure_alcohol
0 Asia Afghanistan ... 0 0.0
1 Europe Albania ... 54 4.9
[2 rows x 6 columns]
"""
The above code snippets and output shows , how it is very easy to operate in Data Frames
Happy Learning @ AMET ODL!!!!
No comments:
Post a Comment