python 2.7 - Reshaping Pandas data frame (a complex case!) -
i want reshape following data frame:
index id numbers 1111 5 58.99 2222 5 75.65 1000 4 66.54 11 4 60.33 143 4 62.31 145 51 30.2 1 7 61.28
the reshaped data frame should following:
id 1 2 3 5 58.99 75.65 nan 4 66.54 60.33 62.31 51 30.2 nan nan 7 61.28 nan nan
i use following code this.
import pandas pd dtframe = pd.read_csv("data.csv") ids = dtframe['id'].unique() temp = dtframe.groupby(['id']) temp2 = {} in ids: temp2[i]= temp.get_group(i).reset_index()['numbers'] dtframe = pd.dataframe.from_dict(temp2) dtframe = dtframe.t
although above code solve problem there more simple way achieve this. tried pivot table not solve problem perhaps requires have same number of element in each group. or may there way not aware of, please share thoughts it.
in [69]: df.groupby(df['id'])['numbers'].apply(lambda x: pd.series(x.values)).unstack() out[69]: 0 1 2 id 4 66.54 60.33 62.31 5 58.99 75.65 nan 7 61.28 nan nan 51 30.20 nan nan
this quite similar doing except loop replaced apply
. pd.series(x.values)
has index default ranges on integers starting @ 0
. index values become column names (above). doesn't matter various groups may have different lengths. apply
method aligns various indices (and fills missing values nan
). convenience!
i learned trick here.
Comments
Post a Comment