简体   繁体   中英

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.

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

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