将数组和元组元素转换为datafram中的列

2024-05-16 06:31:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个看起来像这样的熊猫数据帧(有两行的示例):

    cadd_scores_vec        freqs_vec     CLASS
0  [0.001, -4.053424]  (0.0, 0.0, 0.0)      0
1  [0.001, -3.654581]  (0.0, 0.0, 0.0)      0

我需要将所有内容分解为单个标量列,如下所示:

   col1   col2       col3 col4 col5 col6
0  0.001  -4.053424  0.0  0.0  0.0  0
1  0.001  -3.654581  0.0  0.0  0.0  0

我不太关心新的列名;重要的是将行排列成普通向量,如上图所示。你知道吗

我怎样才能做到呢?你知道吗


Tags: 数据示例内容classcol2col3col1标量
1条回答
网友
1楼 · 发布于 2024-05-16 06:31:01

使用^{}有两种方法:

pd.concat([pd.DataFrame(df[col].values.tolist()) for col in df.columns], axis=1, ignore_index=True)

或者

pd.concat([df[col].apply(pd.Series) for col in df.columns], axis=1, ignore_index=True)

ignore_index=True只是确保不会得到重复的列名。你知道吗

相关问题 更多 >