DATA MANIPULATION WITH PANDAS DATAFRAMES
As a simple example, suppose that we have a two-person company that keeps track of income and expenses on a quarterly basis, and we want to calculate the profit/loss for each quarter, and also the overall profit/loss.
Listing 3.14 displays the contents of pandas_quarterly_df1.py that illustrates how to define a Pandas DataFrame consisting of income-related values.
LISTING 3.14: pandas_quarterly_df1.py
import pandas as pd
summary = {
'Quarter': ['Q1', 'Q2', 'Q3', 'Q4'],
'Cost': [23500, 34000, 57000, 32000],
'Revenue': [40000, 40000, 40000, 40000]
}
df = pd.DataFrame(summary)
print("Entire Dataset:\n",df)
print("Quarter:\n",df.Quarter)
print("Cost:\n",df.Cost)
print("Revenue:\n",df.Revenue)
Listing 3.14 defines the variable summary that contains hard-coded quarterly information about cost and revenue for our two-person...