我有一个数据框,其中一个工资列指定小时工资,一个工会列指定员工是否在工会中。还有其他的变数,但它们现在并不重要。我在找工会里雇员的平均工资。我编写了一个代码,它提供了一个员工是否在工会中的真假列表。不过,我不知道如何申请平均工资。提前谢谢你的帮助。在
#Read cps.csv file
import pandas as pd
cps_df = pd.read_csv('cps.csv')
cps_df
#Function to determine whether or not an employee is in a union
def hourly_wage(x):
""" return true if union else false """
if x['union'] == 'Union':
return True
else:
return False
#Function to create a list of union vs non-union
def union_list(y):
""" return a list determining union vs non-union """
return [hourly_wage(x) for index, x in y.iterrows()]
#Print list
%time
print(union_list(cps_df))
我想你可以用一种更方便的方式来做。熊猫对这类事情很在行。在
假设工资栏是“工资”。然后代码如下所示:
它的基本功能: 1选择所有工会员工(cps_df[cps_df.联合==“联合”]) 2应用程序mean()函数来计算他们的工资(。平均工资())
希望这有帮助。在
相关问题 更多 >
编程相关推荐