Programming newbie - importing output data from a module into plotly wide form input

Hey, so I am new to programming overall but I’ve been doing some data analysis. Okay so I have a module which reads an excel spreadsheet of 3 columns and returns a mean for the 2 output values I want.

I would like to import the mean from this, I’m assuming it’s the function I will be importing this into main() which will then plot a bar chart of wide form input. I have tried looking at how to import the dataframe into main() but I cannot follow it.

Here’s the code for the module:

import pandas as pd


#insert the pathway to the Excel file
Borough = ["Chelsea", "Kensington", "Westminster", "Pimlico", "Bank", "Holborn", "Camden", "Islington", "Angel", "Battersea", "Knightsbridge", "Bermondsey", "Newham"]

file = "/Users//Documents/Ideas/Python Data /realestate70kdata.xlsx"



df = pd.ExcelFile("/Users//Documents/Ideas/Python Data /realestate70kdata.xlsx").parse("realestatemodel")
x = []
x.append(df["Price"])

groupmean = df.groupby('Borough').mean()

print(f"Data mean is: {groupmean}")

Using the following example:

import plotly.express as px
import medianmodeltest1 as mean

df = px.data.mean()

fig = px.bar(df, x="nation", y="amount", color="medal", title="Wide-Form Input")
fig.show()

medianmodeltest1 is the module but the error which is returned is the df = px.data.mean()

Thanks in advance I deeply appreciate it!

Hey @zarathustra welcome to the community!

The command import medianmodeltest1 as mean , imports whatever you have in medianmodeltest1.py file to here as mean.

You also need to specify which variable/class/function you need from medianmodeltest1.py, and you can use . to do that.

For example: let’s say you want to use groupmean variable from medianmodeltest1.py . To do that, you can use a command like this one: mean.groupmean (since you already alias medianmodeltest1 as mean).

Hence, you can do

df = mean.groupmean

By the way, if I were you I would not use a name like mean to do that, since it is also a basic python function it could lead to a confusion. Tbh, I am not sure whether Python would allow you to da at the first place neither…

Anyways,

Another thing that I noticed in your code that you are approaching your module as a callable. You can only use () with callable like function.

Thus coded Zarathustra!

1 Like

Thank you very much, it worked!

Thus coded Zarathustra!

1 Like