# Create a Pandas dataframe from a dictionary
sales_data = {
'date': ['2022-01-01', '2022-01-02', '2022-01-03'],
'units_sold': [100, 120, 115]
sales_df = pd.DataFrame(sales_data)
# Calculate the total sales for each day
sales_df[ total_sales'] = sales_df['units_sold']* 100
# Calculate the average sales per day
average_sales = sales_dfI'total_sales'].mean()
print (f"The average sales per day is: ${average_sales: .2f)")
In this example, we first import the
Pandas library and create a dictionary
called sales_data to hold our data. We
then use the pd.DataFrame( ) function
to create a Pandas dataframe from the
sales_data dictionary.
Next, we calculate the total sales for
each day by creating a new column called
total_sales and assigning it the
product of the units_sold and 100
(assuming each unit is sold for $100). We
then use the mean() function to
calculate the average sales per day and
print the result using the f-string
notation.
This is just a taste of what's possible
with Python and its extensive collectiorn of
libraries and frameworks. Whether you're
building web applications, data pipelines,
machine learning models, or just
exploring the world of programming for
the first time, Python is an excellent
choice that offers powerful features, a
supportive community, and a bright
future.
into
So, what are you waiting for? Dive
Python programming and see where this
exciting anguage takes you!
sales_data = {
'date': ['2022-01-01', '2022-01-02', '2022-01-03'],
'units_sold': [100, 120, 115]
sales_df = pd.DataFrame(sales_data)
# Calculate the total sales for each day
sales_df[ total_sales'] = sales_df['units_sold']* 100
# Calculate the average sales per day
average_sales = sales_dfI'total_sales'].mean()
print (f"The average sales per day is: ${average_sales: .2f)")
In this example, we first import the
Pandas library and create a dictionary
called sales_data to hold our data. We
then use the pd.DataFrame( ) function
to create a Pandas dataframe from the
sales_data dictionary.
Next, we calculate the total sales for
each day by creating a new column called
total_sales and assigning it the
product of the units_sold and 100
(assuming each unit is sold for $100). We
then use the mean() function to
calculate the average sales per day and
print the result using the f-string
notation.
This is just a taste of what's possible
with Python and its extensive collectiorn of
libraries and frameworks. Whether you're
building web applications, data pipelines,
machine learning models, or just
exploring the world of programming for
the first time, Python is an excellent
choice that offers powerful features, a
supportive community, and a bright
future.
into
So, what are you waiting for? Dive
Python programming and see where this
exciting anguage takes you!