简体   繁体   English

寻找更高效的 Pandas 代码使用 BLS 数据集

[英]Looking for a more efficiency pandas code use BLS data set

Looking for a more effective way to prep data for Kmeans analysis.寻找一种更有效的方法来为 Kmeans 分析准备数据。 Using the BLS ( Bureau of Labor Statistics ) and trying to learn Kmeans, I am doing the first pass of the data and want to add two columns, percentage of change over time per in median salary and tote employment.使用 BLS(劳工统计局)并尝试学习 Kmeans,我正在对数据进行第一次传递,并希望添加两列,即工资中位数和托特就业随时间变化的百分比。 The formula is a simply (((current year, state and occ_code) minus the (min of those fourteen data points) / divided by (same min ) ... (( current year, occ_code - min) / min *100), add two columns to dataframe. The data set is ~500K rows by 24 columns. the code is run about 400 rows per min.... current expected to take about 24 hours for a full run, thus the question. Thank you公式很简单(((当前年份,状态和 occ_code)减去(这十四个数据点的最小值)/除以(相同的最小值)...((当前年份,occ_code - min)/ min *100),向数据框添加两列。数据集是 ~500K 行 x 24 列。代码每分钟运行大约 400 行......当前预计需要大约 24 小时才能完整运行,因此问题。谢谢

The sample data is here: Any columns will work, I am just using a_median and tot_emp Unnamed: 0 area st state occ_code occ_title tot_emp emp_prse h_mean a_mean ... h_pct75 h_pct90 a_pct10 a_pct25 a_median a_pct75 a_pct90 year tot_emp_growth a_median_growth 128037 128037 31 NE Nebraska 25-2022 Middle school teachers, except special and voc... 3720 4.9 0 45270 ... 0 0 32850 37160 44220 53170 62120 2008 25.566343 0.0 491755 491755 19 IA Iowa 47-2131 Insulation Workers, Floor, Ceiling, and Wall 360 18 19.59 40750 ... 23.56 27.98 27160 32230 38920 49010 58190 2018 25.566343 0.0 470924 470924 42 PA Pennsylvania 19-1021 Biochemists and Biophysicists 1330 24 43.69 90880 ... 50.04 67.42 52300 64700 84400 104070 140240 2017 25.566343 0.0 267336 267336 20 KS Kansas 39-4031 Morticians, Undertakers, and Funeral Directors 460 25.4 16.6 34540 ... 23.98 27.83 20400 21730 23950 49880 57890 2012 25.566343 0.0 491263 491263 19 IA Iowa 11-9033 Education Administrators, Postsecondary 2360 6.8 51.81 107760 ... 61.22 86.07 52120 68670 93650 127330 179020 2018 25.566343 0.0 5 rows × 24 columns示例数据在这里:任何列都可以使用,我只是使用 a_median 和 tot_emp Unnamed: 0 area st state occ_code occ_title tot_emp emp_prse h_mean a_mean ... h_pct75 h_pct90 a_pct10 a_pct25 a_median a_pct75 a_pct90 year tot_emp_growth a_median_growth 128037 128037 31 NE Nebraska 25-2022 Middle school teachers, except special and voc... 3720 4.9 0 45270 ... 0 0 32850 37160 44220 53170 62120 2008 25.566343 0.0 491755 491755 19 IA Iowa 47-2131 Insulation Workers, Floor, Ceiling, and Wall 360 18 19.59 40750 ... 23.56 27.98 27160 32230 38920 49010 58190 2018 25.566343 0.0 470924 470924 42 PA Pennsylvania 19-1021 Biochemists and Biophysicists 1330 24 43.69 90880 ... 50.04 67.42 52300 64700 84400 104070 140240 2017 25.566343 0.0 267336 267336 20 KS Kansas 39-4031 Morticians, Undertakers, and Funeral Directors 460 25.4 16.6 34540 ... 23.98 27.83 20400 21730 23950 49880 57890 2012 25.566343 0.0 491263 491263 19 IA Iowa 11-9033 Education Administrators, Postsecondary 2360 6.8 51.81 107760 ... 61.22 86.07 52120 68670 93650 127330 179020 2018 25.566343 0.0 5 rows × 24 columns Unnamed: 0 area st state occ_code occ_title tot_emp emp_prse h_mean a_mean ... h_pct75 h_pct90 a_pct10 a_pct25 a_median a_pct75 a_pct90 year tot_emp_growth a_median_growth 128037 128037 31 NE Nebraska 25-2022 Middle school teachers, except special and voc... 3720 4.9 0 45270 ... 0 0 32850 37160 44220 53170 62120 2008 25.566343 0.0 491755 491755 19 IA Iowa 47-2131 Insulation Workers, Floor, Ceiling, and Wall 360 18 19.59 40750 ... 23.56 27.98 27160 32230 38920 49010 58190 2018 25.566343 0.0 470924 470924 42 PA Pennsylvania 19-1021 Biochemists and Biophysicists 1330 24 43.69 90880 ... 50.04 67.42 52300 64700 84400 104070 140240 2017 25.566343 0.0 267336 267336 20 KS Kansas 39-4031 Morticians, Undertakers, and Funeral Directors 460 25.4 16.6 34540 ... 23.98 27.83 20400 21730 23950 49880 57890 2012 25.566343 0.0 491263 491263 19 IA Iowa 11-9033 Education Administrators, Postsecondary 2360 6.8 51.81 107760 ... 61.22 86.07 52120 68670 93650 127330 179020 2018 25.566343 0.0 5 rows × 24 columns

The code currently being tested is:目前正在测试的代码是:

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')

This is cleaner but still slow code for ~500K rows by 25 columns.对于大约 500K 行×25 列的代码,这更清晰但仍然很慢。 It still takes hours.仍然需要几个小时。 If someone has a faster answer please share.如果有人有更快的答案,请分享。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM