简体   繁体   中英

Unusual reshaping of Pandas DataFrame

i have a DF like this:

df = pd.DataFrame({'x': ['a', 'a', 'b', 'b', 'b', 'c'],
                   'y': [1, 2, 3, 4, 5, 6],
                 })

which looks like:

   x  y
0  a  1
1  a  2
2  b  3
3  b  4
4  b  5
5  c  6

I need to reshape it in the way to keep 'x' column unique:

   x    y_1  y_2  y_3
0  a    1    2    NaN
1  b    3    4    5
2  c    6    NaN  NaN

So the max N of 'y_N' columns have to be equal to

max(df.groupby('x').count().values)

and the x column has to contain unique values.

For now i dont get how to get y_N columns.

Thanks.

You can use pandas.crosstab with cumcount column as the columns parameter:

(pd.crosstab(df.x, df.groupby('x').cumcount() + 1, df.y, 
            aggfunc = lambda x: x.iloc[0])
   .rename(columns="y_{}".format).reset_index())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM