我正在做探索性的数据分析,同时这样做我使用相同的代码行很多次。所以我才知道,为什么我不能写这个函数。但我是新来的蟒蛇我不知道如何定义一个函数exactly.So请帮我.....如何在python中编写函数?
的TextData是我的主要数据框和tonumber,smstext是我的变量
# 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
我已经重复了类似的那种码20左右次,所以我想写上面的代码,使我的代码更小的功能。所以请帮助我如何定义。
在此先感谢
[检查这个(https://docs.python.org/2/tutorial/controlflow.html#defining-functions) – 2015-03-31 15:16:50
http://stackoverflow.com/questions/14012076/how-to-write-a-function-that-returns-a-function-in-python – Jonathan 2015-03-31 15:26:02