'How can I split this dataframe in columns
I have a Pandas dataframe with N columns, each column containing a list of 3 values (x, y and z):
I want to get a new dataframe containing 3N columns, each one containing each of the coordinate values for each column (for example, IndexDistalJoint_x, IndexDistalJoint_y and IndexDistalJoint_z, and so on with the remaining original columns).
Any idea? Thanks in advance.
Solution 1:[1]
Consider this DataFrame:
A B C
0 [1, 2, 3] [4, 5, 6] [7, 8, 9]
1 [10, 11, 12] [13, 14, 15] [16, 17, 18]
Then:
data = []
for c in df.columns:
x = df[c].apply(pd.Series)
x.columns = [f"{c}_{ch}" for ch in "xyz"]
data.append(x)
print(pd.concat(data, axis=1))
Prints:
A_x A_y A_z B_x B_y B_z C_x C_y C_z
0 1 2 3 4 5 6 7 8 9
1 10 11 12 13 14 15 16 17 18
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |

