python 分类前百分之十_Python Pandas计算每个类别的回报百分比

我使用了每个访问者的索引值,并检查了下一年相同的索引值(也就是相同的vistor_ID)是否大于0。然后以True或False的形式将其添加到字典中,您可以将其用于条形图。我还列出了两个列表(times\u return和return-at-u-all),用于额外的数据操作。在import pandas as pd

# Part 1, Building the dataframe.

df = pd.DataFrame({

'Visitor_ID':[1,2,3],

'2010' :[4,3,10],

'2011' :[3,0,6],

'2012' :[6,7,3],

'2013' :[0,3,0]

})

df.set_index("Visitor_ID", inplace=True)

# Part 2, preparing the required variables.

def dictionary (max_visitors):

dictionary={}

for x in range(max_visitors):

dictionary["number_{}".format(x)] = []

# print(dictionary)

return dictionary

# Part 3, Figuring out if the customer returned.

def compare_yearly_visits(current_year, next_year):

index = 1

years = df.columns

for x in df[current_year]:

# print (df[years][current_year][index], 'this year.')

# print (df[years][next_year][index], 'Next year.')

how_many_visits = df[years][current_year][index]

did_he_return = df[years][next_year][index]

if did_he_return > 0:

# If the visitor returned, add to a bunch of formats:

returned_at_all.append([how_many_visits, True])

times_returned.append([how_many_visits, did_he_return])

dictionary["number_{}".format(x)].append(True)

else:

## If the visitor did not return, add to a bunch of formats:

returned_at_all.append([how_many_visits, False])

dictionary["number_{}".format(x)].append(False)

index = index +1

# Part 4, The actual program:

highest_amount_of_visits = 11 # should be done automatically, max(visits)?

relevant_years = len(df.columns) -1

times_returned = []

returned_at_all = []

dictionary = dictionary(highest_amount_of_visits)

for column in range(relevant_years):

# print (dictionary)

this_year = df.columns[column]

next_year = df.columns[column+1]

compare_yearly_visits(this_year, next_year)

print ("cumulative dictionary up to:", this_year,"\n", dictionary)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值