私の行の一部を移動して列を作成しようとしていますが、データフレームの大部分を同じに保ちます。データフレームの一部を保持しながら同じ行に表示する行と列を同じ列に表示
結果の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('')
私は好奇心旺盛です。スコア2のType2は、Thing:non-cycleに関連付けられていましたが、結果のデータフレームではThing:bicycleに関連付けられています。これがあなたが望むものだと確信していますか? –
同じ値を持つ複数のIDがある場合、結果のデータフレームにどのIsTrueの値が入るのかをどのように決定しますか?同様のクエリは、Level1、Level2、Time、OAttribute、Thingなどの他の属性にも適用されます。 –
IDが同じ場合は、行が属性(Thing、Level1、Level2、Time、OAttribute、IsTrue)に対して同じ値を持つことが保証されていますか? –