2016-09-10 6 views
0

私の行の一部を移動して列を作成しようとしていますが、データフレームの大部分を同じに保ちます。データフレームの一部を保持しながら同じ行に表示する行と列を同じ列に表示

結果のDATAFRAME:

ID Thing  Level1 Level2 Time OAttribute IsTrue Score Value 
1 bicycle value value 9:30 whatever yes 1  type1 
1 bicycle value value 9:30 whatever yes 2  type2 
2 bicycle value value 2:30 whatever no    
4 non-bic value value 3:30 whatever no 4  type3 
1 bicycle value value 9:30 whatever yes 3  type3 

とは、私はこのような何かしたい:私は試してみました

ID Thing Level1 Level2 Time OAttribute IsTrue Type1 Type2 Type3 
1 bicycle value value 9:30 whatever  yes  1  2  3 
2 bicycle value value 2:30 whatever  yes    
4 non-bic value value 3:30 whatever  no     4 

df_ = df[['Rating', 'Value']].dropna().set_index('Value', append=True).Rating.unstack() 

df.drop('Value', 1).merge(df_, right_index=True, left_index=True, how='left').fillna('') 
+0

私は好奇心旺盛です。スコア2のType2は、Thing:non-cycleに関連付けられていましたが、結果のデータフレームではThing:bicycleに関連付けられています。これがあなたが望むものだと確信していますか? –

+0

同じ値を持つ複数のIDがある場合、結果のデータフレームにどのIsTrueの値が入るのかをどのように決定しますか?同様のクエリは、Level1、Level2、Time、OAttribute、Thingなどの他の属性にも適用されます。 –

+0

IDが同じ場合は、行が属性(Thing、Level1、Level2、Time、OAttribute、IsTrue)に対して同じ値を持つことが保証されていますか? –

答えて

1

中間データフレームを作成し、外側マージを使用する方法もあります。中間データフレームを計算する

In [102]: df 
Out[102]: 
    ID Thing Level1 Level2 Time OAttribute IsTrue Score Value 
0 1 bicycle value value 9:30 whatever yes 1.0 type1 
1 1 bicycle value value 9:30 whatever yes 2.0 type2 
2 2 bicycle value value 2:30 whatever  no NaN NaN 
3 4 non-bic value value 3:30 whatever  no 4.0 type3 
4 1 bicycle value value 9:30 whatever yes 3.0 type3 

In [103]: dg = pd.DataFrame(columns=pd.np.append(df['Value'].dropna().unique(), ['ID'])) 

In [104]: for i in range(len(df)): 
    ...:  key = df.loc[i]['Value'] 
    ...:  value = df.loc[i]['Score'] 
    ...:  ID = df.loc[i]['ID'] 
    ...:  if key is not pd.np.nan: 
    ...:   dg.loc[i, key] = value 
    ...:   dg.loc[i, 'ID'] = ID 
    ...:     

In [105]: dg 
Out[105]: 
    type1 type2 type3 ID 
0  1 NaN NaN 1 
1 NaN  2 NaN 1 
3 NaN NaN  4 4 
4 NaN NaN  3 1 

In [106]: dg.groupby('ID').max().reset_index() 

In [107]: dg 
Out[107]: 
    ID type1 type2 type3 
0 1  1  2  3 
1 4 NaN NaN  4 

In [108]: df[df.columns.difference(['Score', 'Value'])].drop_duplicates().merge(dg, how='outer').fillna('') 
Out[108]: 
    ID IsTrue Level1 Level2 OAttribute Thing Time type1 type2 type3 
0 1 yes value value whatever bicycle 9:30  1  2  3 
1 2  no value value whatever bicycle 2:30     
2 4  no value value whatever non-bic 3:30     4 

もう一つの方法は、forループを回避し、unstack()を使用して次のようになります。

In [150]: df 
Out[150]: 
    ID Thing Level1 Level2 Time OAttribute IsTrue Score Value 
0 1 bicycle value value 9:30 whatever yes 1.0 type1 
1 1 bicycle value value 9:30 whatever yes 2.0 type2 
2 2 bicycle value value 2:30 whatever  no NaN NaN 
3 4 non-bic value value 3:30 whatever  no 4.0 type3 
4 1 bicycle value value 9:30 whatever yes 3.0 type3 

In [151]: dg = df[['Score', 'Value']].dropna().set_index('Value', append=True).Score.unstack().join(df['ID']).groupby('ID').max().reset_index() 

In [152]: df[df.columns.difference(['Score', 'Value'])].drop_duplicates().merge(dg, how='outer').fillna('') 
Out[152]: 
    ID IsTrue Level1 Level2 OAttribute Thing Time type1 type2 type3 
0 1 yes value value whatever bicycle 9:30  1  2  3 
1 2  no value value whatever bicycle 2:30     
2 4  no value value whatever non-bic 3:30     4 
+0

私はそれが私に合計を与えることを望んでいないが、私がそれを取る場合は、タイプGroupBy – Kalimantan

+0

とマージできない@カリマンタンsum()は実際にはここでのみ使用されています。例:1 + NaN + NaNは1を返します。私は他の合計を計算していません。 –

+0

いくつかの列には16がありますが、さらに私の列の値の1つが省略されています – Kalimantan

0

は本当にあなたが何をしようとして伝えることはできませんスコアと値の両方の列が同時に表示されます。

しかし、あなたはあなたの "値"列を変換するために探している場合は、あなたの "値"列のワンホットエンコーディングのようなものを探しているとパンダは非常に便利な機能を備えています。あなたがしなければならないです:あなたは3つの新しい列、すなわち[TYPE1、TYPE2、TYPE3]で新しいデータフレームを与える

pd.get_dummies(df['Value']) 

は、1と0の束を満たしました。

その後、.joinコマンドを使用して元のdfに戻すだけで済みます。その後、必要のない列を削除することができます。

+0

私はそれを混乱させた誤植を残していた – Kalimantan

関連する問題