Python reads an excel file, in which a column is of string type (such as 3-5, 5-10). How to split it into numerical values and calculate the average

CSDN Q & A 2022-01-15 04:10:48 阅读数:778

python reads excel file column

img

 Workplace Number of recruiters Educational background Years of service Job category Release Time type seek Hangzhou technical survey center full-time 2019- Ben 3 04-08 5 try Families, 3 Hangzhou Products Co., Ltd full-time 2019- Ben 1、 Product synthesis 04-15 Address Families, Hangzhou e-commerce Co., Ltd whole 2019- Ben 5 1. United 03-29 10 Families, Operator management 

Want to calculate the average number of years of service in different positions , The column of working years is numerical , Ask what to do




Refer to the answer 1:
>>> from numpy import *>>> import pandas as pd>>> data = pd.read_table('test.txt')>>> data price0 1-31 3-62 5-83 8-114 4-95 7-15>>> def getmean(rawList):...  meanValue = mean([int(i) for i in rawList])...  return meanValue>>> data['mean'] = data['price'].str.split('-').apply(lambda x:getmean(x))>>> data price mean0 1-3 2.01 3-6 4.52 5-8 6.53 8-11 9.54 4-9 6.55 7-15 11.0

img




Refer to the answer 2:
版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2021/12/202112112245050810.html