import pandas as pd
url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/drinks.csv'
df = pd.read_csv(url).head(5).copy()
df
country | beer_servings | spirit_servings | wine_servings | total_litres_of_pure_alcohol | continent | |
---|---|---|---|---|---|---|
0 | Afghanistan | 0 | 0 | 0 | 0.0 | AS |
1 | Albania | 89 | 132 | 54 | 4.9 | EU |
2 | Algeria | 25 | 0 | 14 | 0.7 | AF |
3 | Andorra | 245 | 138 | 312 | 12.4 | EU |
4 | Angola | 217 | 57 | 45 | 5.9 | AF |
For each of the following lines of code:
df
df.continent
df['continent']
df[['country', 'continent']]
df[[False, True, False, True, False]]
df
country | beer_servings | spirit_servings | wine_servings | total_litres_of_pure_alcohol | continent | |
---|---|---|---|---|---|---|
0 | Afghanistan | 0 | 0 | 0 | 0.0 | AS |
1 | Albania | 89 | 132 | 54 | 4.9 | EU |
2 | Algeria | 25 | 0 | 14 | 0.7 | AF |
3 | Andorra | 245 | 138 | 312 | 12.4 | EU |
4 | Angola | 217 | 57 | 45 | 5.9 | AF |
print type(df)
print df.shape
<class 'pandas.core.frame.DataFrame'> (5, 6)
df.continent
0 AS 1 EU 2 AF 3 EU 4 AF Name: continent, dtype: object
print type(df.continent)
print df.continent.shape
<class 'pandas.core.series.Series'> (5L,)
df['continent']
0 AS 1 EU 2 AF 3 EU 4 AF Name: continent, dtype: object
print type(df['continent'])
print df['continent'].shape
<class 'pandas.core.series.Series'> (5L,)
df[['country', 'continent']]
country | continent | |
---|---|---|
0 | Afghanistan | AS |
1 | Albania | EU |
2 | Algeria | AF |
3 | Andorra | EU |
4 | Angola | AF |
print type(df[['country', 'continent']])
print df[['country', 'continent']].shape
<class 'pandas.core.frame.DataFrame'> (5, 2)
# equivalent
cols = ['country', 'continent']
df[cols]
country | continent | |
---|---|---|
0 | Afghanistan | AS |
1 | Albania | EU |
2 | Algeria | AF |
3 | Andorra | EU |
4 | Angola | AF |
df[[False, True, False, True, False]]
country | beer_servings | spirit_servings | wine_servings | total_litres_of_pure_alcohol | continent | |
---|---|---|---|---|---|---|
1 | Albania | 89 | 132 | 54 | 4.9 | EU |
3 | Andorra | 245 | 138 | 312 | 12.4 | EU |
print type(df[[False, True, False, True, False]])
print df[[False, True, False, True, False]].shape
<class 'pandas.core.frame.DataFrame'> (2, 6)
# equivalent
df[df.continent=='EU']
country | beer_servings | spirit_servings | wine_servings | total_litres_of_pure_alcohol | continent | |
---|---|---|---|---|---|---|
1 | Albania | 89 | 132 | 54 | 4.9 | EU |
3 | Andorra | 245 | 138 | 312 | 12.4 | EU |