Python 寻找更高效的代码使用BLS数据集
寻找一种更有效的方法为Kmeans分析准备数据。使用劳工统计局(Bureau of Labor Statistics)并尝试学习Kmeans,我正在进行第一次数据传递,并希望添加两列,即工资中位数随时间变化的百分比和托特就业率。公式是一个简单的((当前年份、状态和occ_代码)减去(这十四个数据点的最小值)/除以(相同的最小值)…((当前年份、occ_代码-最小值)/min*100),在dataframe中添加两列。数据集为~500K行乘24列。代码运行速度约为每分钟400行……当前运行预计需要24小时左右,因此,这就是问题所在。谢谢 示例数据如下: 任何列都可以,我只是使用了一个中位数和tot empPython 寻找更高效的代码使用BLS数据集,python,pandas,performance,large-data,Python,Pandas,Performance,Large Data,寻找一种更有效的方法为Kmeans分析准备数据。使用劳工统计局(Bureau of Labor Statistics)并尝试学习Kmeans,我正在进行第一次数据传递,并希望添加两列,即工资中位数随时间变化的百分比和托特就业率。公式是一个简单的((当前年份、状态和occ_代码)减去(这十四个数据点的最小值)/除以(相同的最小值)…((当前年份、occ_代码-最小值)/min*100),在dataframe中添加两列。数据集为~500K行乘24列。代码运行速度约为每分钟400行……当前运行预计需要
未命名:0区圣州occ代码occ名称tot_emp emp prse h_平均…h_pct75 h_pct90 a_pct10 a_pct25 a_pct75 a_pct90年tot emp增长a_中位增长
128037 128037 31东北内布拉斯加州25-2022中学教师,特殊和voc除外…3720 4.9 0 45270…0 32850 37160 44220 53170 62120 2008 25.566343 0.0
491755 491755 19 IA爱荷华州47-2131保温工人、地板、天花板和墙壁360 18 19.59 40750…23.56 27.98 27160 32230 38920 49010 58190 2018 25.566343 0.0
470924 470924 42宾夕法尼亚州19-1021生物化学家和生物物理学家1330 24 43.69 90880…50.04 67.42 52300 64700 84400 104070 140240 2017 25.566343 0.0
267336 267336 20 KS堪萨斯39-4031殡仪员、殡仪员和殡仪主管460 25.4 16.6 34540…23.98 27.83 20400 21730 23950 49880 57890 2012 25.566343 0.0
491263 491263 19 IA爱荷华州11-9033教育管理员,专上教育2360 6.8 51.81 107760…61.22 86.07 52120 68670 93650 127330 179020 2018 25.566343 0.0
5行×24列
目前正在测试的代码是:
def occ_code_growths(df):
for i in range(len(df)):
cols_lit = ['year', 'occ_code', 'st' , 'tot_emp', 'a_median']
df_lookup = df.lookup(list([df.index[i]]*len(cols_lit)), cols_lit)
idx_emp_min = df[(df['occ_code'] == df_lookup[1]) & (df['st'] == df_lookup[2]) ]\
['tot_emp'].values.astype(int).min()
idx_median_min = df[(df['occ_code'] == df_lookup[1]) & (df['st'] == df_lookup[2]) ]\
['a_median'].values.astype(int).min()
idx_emp = df[(df['occ_code'] == df_lookup[1]) & (df['st'] == df_lookup[2]) \
& (df['year'] == df_lookup[0]) ]['tot_emp'].values.astype(int)
idx_median = df[(df['occ_code'] == df_lookup[1]) & (df['st'] == df_lookup[2]) \
& (df['year'] == df_lookup[0]) ]['a_median'].values.astype(int)
df['tot_emp_growth'] = float((((idx_emp - idx_emp_min) / idx_emp_min) * 100)[0])
df['a_median_growth'] = float((((idx_median - idx_median_min) / idx_median_min) * 100)[0])
if i % 200 == 0 :
print(df.index[i])
return(df)
df_4 = occ_code_growths(df)
df_4.to_csv('./data/kmeans.csv')
这是一个更干净的代码,但是对于大约500K行乘25列的代码来说仍然很慢。它仍然需要几个小时。如果有人有更快的答案,请分享
df_6 =[[]]
def occ_code_growths(df, df_6):
df_occ_unique = df.occ_code.unique()
# print(df_occ_unique)
df_st_unique = df.st.unique()
# print(df_st_unique)
df_year_unique = df.year.unique()
# print(df_yr_unique)
df_6 = pd.DataFrame({ 'idx_row': [], 'tot_growth': [], 'median_growth': [], 'code': [], 'st': [], 'yr': []})
# for i in range(len(df)):
# print('i',i)
for code in range(len(df_occ_unique)):
# print('code',code)
for st in range(len(df_st_unique)):
# print('st',st)
try:
idx_emp_min = df[(df['occ_code'] == df_occ_unique[code]) & (df['st'] == df_st_unique[st]) ]['tot_emp'].values.astype(int).min()
idx_median_min = df[(df['occ_code'] == df_occ_unique[code]) & (df['st'] == df_st_unique[st]) ]['a_median'].values.astype(int).min()
except:
print('Error with', tot_emp_growth, a_median_growth, df_occ_unique[code], df_st_unique[st], df_year_unique[yr].astype(int) )
for yr in range(len(df_year_unique)):
# print('yr',yr)
try:
idx_emp = df[(df['occ_code'] == df_occ_unique[code]) & (df['st'] == df_st_unique[st]) \
& (df['year'] == df_year_unique[yr]) ]['tot_emp'].values.astype(int)
idx_median = df[(df['occ_code'] == df_occ_unique[code]) & (df['st'] == df_st_unique[st]) \
& (df['year'] == df_year_unique[yr]) ]['a_median'].values.astype(int)
idx_row = df[(df['occ_code'] == df_occ_unique[code]) & (df['st'] == df_st_unique[st]) \
& (df['year'] == df_year_unique[yr]) ].index.values.astype(int)
except:
print('Error with', tot_emp_growth, a_median_growth, df_occ_unique[code], df_st_unique[st], df_year_unique[yr].astype(int) )
try:
tot_emp_growth = float((((idx_emp - idx_emp_min) / idx_emp_min) * 100)[0])
a_median_growth = float((((idx_median - idx_median_min) / idx_median_min) * 100)[0])
df_6 = df_6.append({'idx_row': idx_row, 'tot_growth': tot_emp_growth, \
'median_growth': a_median_growth, 'code': df_occ_unique[code], \
'st': df_st_unique[st], 'yr': df_year_unique[yr].astype(int) }, ignore_index=True)
except:
print('Error with', tot_emp_growth, a_median_growth, df_occ_unique[code], df_st_unique[st], df_year_unique[yr].astype(int) )
# print(df_6)
# if i % 200 == 0 :
# print(df.index[i])
return(df, df_6)
df_5, df_7 = occ_code_growths(df, df_6)