row to columns while keeping part of dataframe, display on same row
<p>I am trying to move some of my rows and make the them columns, but keep a large portion of the dataframe the same.</p>

<p>Resulting Dataframe:</p>

<pre><code>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
</code></pre>

<p>and I want something like this:</p>

<pre><code>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
</code></pre>

<p>I have tried</p>

<pre><code>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('')
</code></pre>

<p>Can't really tell what you're trying to do with both of your Score and Value columns at the same time. </p>

<p>But if you're looking to transform your "Value" column, you're looking for something like one-hot encoding of your "Value" column and pandas has a very convenient function for it. All you have to do is:</p>

<pre><code>pd.get_dummies(df['Value'])
</code></pre>

<p>That will give you a new data frame with 3 new columns namely [type1,type2,type3] filled a bunch of 1s and 0s.</p>

<p>After that, all you have to do is use the .join command to join it back to your original df. You can then proceed to delete the columns that you don't need. </p>

python pandas dataframe