2

I am doing exploratory data analysis, while doing that i am using the same lines of code many times .So i came to know that why can't i wrote the function for that.But i am new to python i don't know how to define a function exactly.So please help me.....

textdata is my main dataframe and tonumber,smstext are my variables

# subsetting the textdata
mesbytonum = textdata[['tonumber', 'smstext']]
# calculating the no.of messages by tonumber
messbytonum_freq = mesbytonum.groupby('tonumber').agg(len)
# resetting the index
messbytonum_freq.reset_index(inplace=True)
# making them in a descending order
messbytonum_freq_result = messbytonum_freq.sort(['smstext'], ascending=[0])
#calcuating percentages
messbytonum_freq_result['percentage'] = messbytonum_freq_result['smstext']/sum(messbytonum_freq_result['smstext'])
# considering top10
top10tonum = messbytonum_freq_result.head(10)
# top10tonum  

i have repeated the similar kind of code around 20 times so i want to write the function for the above code which makes my code smaller. So please help me how can i define.

Thanks in advance

2

1 Answer 1

4

The function is defined like this:

def func(arg1, arg2, argN):
    # do something
    # you may need to return value(s) too

And called like this:

func(1,2,3) # you can use anything instead of 1,2 and 3

It will be

def MyFunc(textdata):
    mesbytonum = textdata[['tonumber', 'smstext']]
    messbytonum_freq = mesbytonum.groupby('tonumber').agg(len)
    messbytonum_freq.reset_index(inplace=True)
    messbytonum_freq_result = messbytonum_freq.sort(['smstext'], ascending=[0])
    messbytonum_freq_result['percentage'] = messbytonum_freq_result['smstext']/sum(messbytonum_freq_result['smstext'])
    top10tonum = messbytonum_freq_result.head(10)
    return   # what do you want to return?

# use this function
result=MyFunc(<argument here>)

# then you need to use result somehow

Your function can also return multiple values

return spam, egg

which you have to use like this

mySpam, myEgg=MyFunction(<argument>)
Sign up to request clarification or add additional context in comments.

3 Comments

To be complete, you will get the result by calling result = MyFunc(<parameter>). result will then be set equal to whatever you put after return in the function.
you are using ((mesbytonum = textdata[['tonumber', 'smstext']])) command inside the function ,some times my variables are different,if i give the data directly it will always calculate for the operator.so please help me in that way.please edit your given answer if you can. Thank you very much .
@suri1617, just add some more arguments to this function and use them inside

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.