简体   繁体   中英

Transform a 3 columns (x, y, result) Python Pandas DataFrame to a DataFrame of result values with x (unique) as row and y (unique) as column

I would like to transform a Python Pandas DataFrame df like:

      x  y  result
id                
1  -0.8 -1    0.64
2  -0.8  0   -0.36
3  -0.4 -1    0.16
4  -0.4  0   -0.84
5   0.0 -1    0.00
6   0.0  0   -1.00
7   0.4 -1    0.16
8   0.4  0   -0.84
9   0.8 -1    0.64
10  0.8  0   -0.36

to a DataFrame like this:

        -1     0
-0.8  0.64 -0.36
-0.4  0.16 -0.84
 0.0     0 -1.00
 0.4  0.16 -0.84
 0.8  0.64 -0.36

I know how to get unique x values:

df["x"].unique()

and unique y values with:

df["y"].unique()

but I don't know how to "distribute" result column values inside DataFrame.

I would prefer a vectorized solution in order to avoid for loops.

That is a pivot operation, you can either use .pivot_table :

>>> df.pivot_table(values='result', index='x', columns='y')
y       -1     0
x               
-0.8  0.64 -0.36
-0.4  0.16 -0.84
 0.0  0.00 -1.00
 0.4  0.16 -0.84
 0.8  0.64 -0.36

or .pivot :

>>> df.pivot(index='x', columns='y')['result']
y       -1     0
x               
-0.8  0.64 -0.36
-0.4  0.16 -0.84
 0.0  0.00 -1.00
 0.4  0.16 -0.84
 0.8  0.64 -0.36

or .groupby followed by .unstack :

>>> df.groupby(['x', 'y'])['result'].aggregate('first').unstack()
y       -1     0
x               
-0.8  0.64 -0.36
-0.4  0.16 -0.84
 0.0  0.00 -1.00
 0.4  0.16 -0.84
 0.8  0.64 -0.36

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