@@ -75,19 +75,14 @@ plt.ylabel('GDP per capita')
7575~~~
7676{: .python}
7777![ GDP barplot for Australia] ( ../fig/9_gdp_bar.png )
78- * Extract years from the last four characters of the columns' names.
79- * Store these in a list using the Accumulator pattern.
80- * Can also convert dataframe data to a list.
78+ * Extract years from the last four characters of the column names using dataframes function.
8179
8280~~~
83- # Accumulator pattern to collect years (as character strings).
84- years = []
85- for col in data.columns:
86- year = col[-4:]
87- years.append(year)
81+ #dataframes function for string manipulation
82+ years = data.columns.str.strip('gdpPercap_')
8883
89- # Australia data as list.
90- gdp_australia = data.loc['Australia'].tolist()
84+ # Australia data
85+ gdp_australia = data.loc['Australia']
9186
9287# Plot: 'g--' sets the line style.
9388plt.plot(years, gdp_australia, 'g--')
@@ -97,15 +92,6 @@ plt.plot(years, gdp_australia, 'g--')
9792## Can plot many sets of data together.
9893
9994~~~
100- # Accumulator pattern to collect years (as character strings).
101- years = []
102- for col in data.columns:
103- year = col[-4:]
104- years.append(year)
105- # For loops is not introduced yet
106- # Instead of for loop we can use pandas dataframe functions to get the year.
107- years = data.columns.str.strip('gdpPercap_')
108-
10995# Select two countries' worth of data.
11096gdp_australia = data.loc['Australia']
11197gdp_nz = data.loc['New Zealand']
0 commit comments