首页  >  文章  >  后端开发  >  如何将 Pandas Dataframe 列中的元组拆分为单独的列?

如何将 Pandas Dataframe 列中的元组拆分为单独的列?

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-10-25 11:08:31657浏览

How do you Split Tuples in a Pandas Dataframe Column into Individual Columns?

在 Pandas Dataframe 中拆分元组的列

在 Pandas Dataframe 中,经常会遇到包含元组的列,例如提供的示例:

<code class="python">>>> d1
   y norm test  y norm train  len(y_train)  len(y_test)  \
0    64.904368    116.151232          1645          549
1    70.852681    112.639876          1645          549

                                    SVR RBF  \
0   (35.652207342877873, 22.95533537448393)
1  (39.563683797747622, 27.382483096332511)

                                        LCV  \
0  (19.365430594452338, 13.880062435173587)
1  (19.099614489458364, 14.018867136617146)

                                   RIDGE CV  \
0  (4.2907610988480362, 12.416745648065584)
1    (4.18864306788194, 12.980833914392477)

                                         RF  \
0   (9.9484841581029428, 16.46902345373697)
1  (10.139848213735391, 16.282141345406522)

                                           GB  \
0  (0.012816232716538605, 15.950164822266007)
1  (0.012814519804493328, 15.305745202851712)

                                             ET DATA
0  (0.00034337162272515505, 16.284800366214057)  j2m
1  (0.00024811554516431878, 15.556506191784194)  j2m</code>

要将这些列拆分为元组中每个元素的单独列,您可以使用以下技术:

<code class="python"># Convert column to list of tuples
col_to_split = df['column_name'].tolist()

# Create a new dataframe from the list of tuples
split_col = pd.DataFrame(col_to_split, index=df.index)

# Assign new columns to original dataframe
df[['column_name_a', 'column_name_b']] = split_col</code>

例如,在提供的数据框中,您可以将 LCV 列拆分为 LCV- a 和 LCV-b 列:

<code class="python">d1[['LCV-a', 'LCV-b']] = pd.DataFrame(d1['LCV'].tolist(), index=d1.index)</code>

这将产生以下数据框:

<code class="python">>>> d1
   y norm test  y norm train  len(y_train)  len(y_test)  \
0    64.904368    116.151232          1645          549
1    70.852681    112.639876          1645          549

                                    SVR RBF  \
0   (35.652207342877873, 22.95533537448393)
1  (39.563683797747622, 27.382483096332511)

                                        LCV-a  LCV-b
0  (19.365430594452338, 13.880062435173587)  None
1  (19.099614489458364, 14.018867136617146)  None

                                   RIDGE CV  \
0  (4.2907610988480362, 12.416745648065584)
1    (4.18864306788194, 12.980833914392477)

                                         RF  \
0   (9.9484841581029428, 16.46902345373697)
1  (10.139848213735391, 16.282141345406522)

                                           GB  \
0  (0.012816232716538605, 15.950164822266007)
1  (0.012814519804493328, 15.305745202851712)

                                             ET DATA
0  (0.00034337162272515505, 16.284800366214057)  j2m
1  (0.00024811554516431878, 15.556506191784194)  j2m</code>

以上是如何将 Pandas Dataframe 列中的元组拆分为单独的列?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn