I am trying to create a function in which i want to pass pandas dataframe's column name.
Basically i am using plotly.express and that is what i am embedding into a function.
But when i am doing so, it is saying that plotly requires column name but no colomn name found.
def bar(df,**kwargs):
val=", ".join(f"{key}={value}" for key, value in kwargs.items())
px.bar(df,val).show()
bar(sales,x='Category',y='Quantity')```
How can i solve this. So that whenever user passes an argument it consider it as a field not just a string.
Any leads will be helpful.
TIA
CodePudding user response:
This is not really a plotly question, but python. Fundamentals of how argument passing works.
Simply you just need to pass the kwargs pass to your function to px.bar()
import plotly.express as px
import pandas as pd
import numpy as np
def bar(df,**kwargs):
px.bar(df,**kwargs).show()
sales = pd.DataFrame({"Category":np.random.choice(list("ABCD"), 20), "Quantity":np.random.randint(1,10,20)})
bar(sales,x='Category',y='Quantity')
