Adding stats code to a function in Python

2024/11/16 10:42:52

Im relatively new to Python and trying to learn how to write functions. The answer to this post highlights how to get certain stats from a dataframe and I would like to use it in a function.

This is my attempt but it is not working with an AttributeError: 'SeriesGroupBy' object has no attribute 'test_for_B':

 def test_multi_match(df_in,test_val):test_for_B = df_in == test_valcontigious_groups = ((df_in == test_val) & (df_in != df_in.shift())).cumsum() + 1counts = df_in.groupby(contigious_groups).test_for_B.sum()counts.value_counts() / contigious_groups.max()

Can someone please help put this code in a function I can re use on other data frames? Thanks.

Edit: Removed large attribute error now this has been answered.

Answer

Here you go:

def repeat_stats(series, var):isvar = series == varwasntvar = series != series.shift()cont_grps = (isvar & wasntvar).cumsum()counts = isvar.loc[cont_grps.astype(bool)].groupby(cont_grps).sum()return counts.value_counts() / cont_grps.max()repeat_stats(rng.initial_data, 'B')3.0    0.5
2.0    0.5
Name: initial_data, dtype: float64
https://en.xdnf.cn/q/119206.html

Related Q&A

Multiple Histograms, each for a label of x-axis, on the same graph matplotlib

I am trying to plot a graph to show different behaviors by males and females with regards to a certain activity, for different age-groups. So, if the age groups are: [1-10,11-20,21-30...] I would like …

How can I split a string in Python? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicates:Split python string every nth character?What is the most “pythonic” way to iterate over a list in chu…

How to enable media device access in Edge browser using Selenium?

I have a Selenium test I want to run in Edge against a page which uses the webcam and microphone. In order for those media devices to work in the browser, the user has to allow the site access to the d…

Print specific rows that are common between two dataframes

i have a dataframe (df1) like this id link 1 google.com 2 yahoo.com 3 gmail.comi have another dataframe(df2) like this: id link numberOfemployees 1 linkedin.com …

What is the problem with buttons connection in PyQt5?

I have the problem that I cannot connect the implementation between two buttons. After I pressed the "Grayscale" button and got the grayscale image, I pressed the "Canny" button but…

Install ipykernel in vscode - ipynb (Jupyter)

Greetings! I am not able to install ipykernel and receiving following error. Please advise. print(Hello) Running cells with Python 3.10.5 64-bit requires ipykernel package. Run the following command to…

How do I write a form in django

I am writing a forums app for my final project in class and I am trying to write a form for creating a new thread, Basically all I need it to do is username: text box hereName of thread: Text box here…

Whats the difference between namespaces and names?

Im assuming the namespace is the allotted place in memory in which the name is to be stored. Or are they the same thing?

Finding a string in python and writing it in another file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Python error TypeError: function takes exactly 1 argument (5 given)

Traceback (most recent call last):File "wdd.py", line 164, in <module>file.write("temperature is ", temperature, "wet is ", humidity, "%\n") TypeError: fun…