2017-06-28 21 views
3

私はこれを間違った方法で行っているかもしれませんが、データの分析を計画しているので、各アプリケーションごとに1つのエントリが必要です。列の数値に基づいて行を追加してください

マイデータフレームは、このようなものになります。

ID Job Title Number Applied Hired Feature(Math) 
1 Accountant    3  2    1 
2 Marketing    1  1    0 
3  Finance    1  1    1 

私はそれがこのように見えるようにする必要があります(0、はい= 1 =なし):

ID Job Title Number Applied Hired Feature(Math)  
1 Accountant    1  0    1 
2 Accountant    1  1    1 
3 Accountant    1  1    1 
4 Marketing    1  1    0 
5  Finance    1  1    1 

私は、行を追加する必要がありません申請者ごとにNumber Appliedは常に1にする必要があります。これが完了したらNumber Appliedの列を削除できます。

私が含まなかった追加機能があります。分析のポイントは、スキルセットに基づいて人が仕事を見つけるかどうかを予測する機械学習アルゴリズムを適用することです。私の現在のデータフレームは、雇用をyesまたはnoに変換すると、3の代わりに2人しか数学スキルを持つ人しか雇われていないと考えられるので、私の現在のデータフレームは機能しません。

答えて

1

これは、集められたサンプルのあなたはその後、

unroll(df, 'Number Applied', 'Hired') 
Feature(Math) Hired ID Job Title Number Applied 
0    1  1 1 Accountant    1 
1    1  1 1 Accountant    1 
2    1  0 1 Accountant    1 
3    0  1 2 Marketing    1 
4    1  1 3  Finance    1 
+0

を行うことができます

from itertools import imap, izip def iterdicts(df): """ Utility to iterate over rows of a data frame as dictionaries. """ col = df.columns for row in df.itertuples(name=None, index=False): yield dict(zip(col, row)) def deaggregate(dicts, *columns): """ Deaggregate an iterable of dictionaries 'dicts' where the numbers in 'columns' are assumed to be aggregated counts. """ for row in dicts: for i in xrange(max(row[c] for c in columns)): d = dict(row) # replace each count by a 0/1 indicator d.update({c: int(i < row[c]) for c in columns}) yield d def unroll(df, *columns): return pd.DataFrame(deaggregate(iterdicts(df), *columns)) 

ありがとうございました。それは魅力のように働いた。私自身がこれほどエレガントな解決策を思いつきたいと思っています。 – Srule

+1

問題ありません。投稿された回答があなたのために働いた場合は、それを受け入れてください(回答スコアの下のチェックマークをクリックしてください)。 –

+1

@Sruleあなたは15以上の新しい評判でアップ票を投じることもできます... [** WhatToDo **](https://stackoverflow.com/help/someone-answers) – piRSquared

1
d1 = df.loc[df.index.repeat(df['Number Applied'])] 

hired = (
    d1.groupby('Job Title').cumcount() >= 
     d1['Number Applied'] - d1['Hired'] 
).astype(int) 

d1.assign(**{'Number Applied': 1, 'Hired': hired}) 

    ID Job Title Number Applied Hired Feature(Math) 
0 1 Accountant    1  0    1 
0 1 Accountant    1  1    1 
0 1 Accountant    1  1    1 
1 2 Marketing    1  1    0 
2 3  Finance    1  1    1 
+0

お詫び申し上げます!説明する時間がありません。 – piRSquared

関連する問題