pandas - Transposing data in dataframe with multiple rows per ID -


i have dataframe 2 columns: id , value. each id appears in many rows unique values.

there 2 values i'm interested in logging, 2 & 39. instead of having 1 row per value i'd create new dataframe 3 columns: id, value2, value39. value2 , value39 need boolean values indicate whether or not registered in original dataframe.

thanks help.

edit: i'd have dataframe 1 row per id. means need consolidate value2 , value39 boolean value 1 row.

create second dataframe based on id column on first dataframe, , create 2 columns testing whether or not value 2 or 39.

df = pd.dataframe({'id': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 6, 6: 6},                    'value': {0: 2, 1: 2, 2: 39, 3: 39, 4: 1, 5: 39, 6: 2}})  df2 = df[['id']] df2['value2'] = df.value == 2 df2['value39'] = df.value == 39  edit: >>> df2.groupby(['id']).any()    value2 value39 id                0    true   false 1    true   false 2   false    true 3   false    true 4   false   false 6    true    true 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -