如何将包含对象的列拆分成多个列?

huangapple go评论76阅读模式
英文:

How can I split a column that has an object onto multiple columns

问题

我可以帮你处理这个问题。你可以使用Python中的pandas库来实现这个操作。以下是你可以使用的代码:

import pandas as pd

# 创建包含你的数据的DataFrame
data = {
    'A': [1, 3],
    'B': [2, 4],
    'objectToBeSplit': [{0.223, 0.112, 0.441}, {0.423, 0.402, 0.593}]
}

df = pd.DataFrame(data)

# 将'objectToBeSplit'列拆分成多列
df[['C', 'D', 'E']] = pd.DataFrame(df['objectToBeSplit'].tolist(), index=df.index)

# 删除原始的'objectToBeSplit'列
df = df.drop('objectToBeSplit', axis=1)

# 打印结果
print(df)

这段代码会将你的数据转换成你想要的格式。

英文:

my columns look like this:

A  B     objectToBeSplit
1  2  {0.223, 0.112, 0.441}
3  4  {0.423, 0.402, 0.593}

And I would like to have it like this:

A  B    C      D      E
1  2  0.223  0.112  0.441
3  4  0.423  0.402  0.593

How can I split the objectToBeSplit column in python?

答案1

得分: 1

以下是翻译好的部分:

有几种方法可以实现这一点

# 示例DataFrame
df = pd.DataFrame({
    'A': [1, 3],
    'B': [2, 4],
    'objectToBeSplit': ['{0.223, 0.112, 0.441}', '{0.423, 0.402, 0.593}']
})

# 分割 'objectToBeSplit' 列
df[['C', 'D', 'E']] = df['objectToBeSplit'].str.strip('{}').str.split(',', expand=True)

# 删除原始 'objectToBeSplit' 列
df = df.drop('objectToBeSplit', axis=1)

方法2:

# 通过分割 "objectToBeSplit" 列中的值创建新列 C、D、E
df[['C', 'D', 'E']] = pd.DataFrame(df['objectToBeSplit'].tolist()).applymap(float)

# 删除原始 'objectToBeSplit' 列
df = df.drop('objectToBeSplit', axis=1)

# 打印更新后的 DataFrame
print(df)
英文:

There are a couple of ways to achieve this

# Sample DataFrame
df = pd.DataFrame({
    'A': [1, 3],
    'B': [2, 4],
    'objectToBeSplit': ['{0.223, 0.112, 0.441}', '{0.423, 0.402, 0.593}']
})

# Splitting the 'objectToBeSplit' column
df[['C', 'D', 'E']] = df['objectToBeSplit'].str.strip('{}').str.split(', ', expand=True)

# Dropping the original 'objectToBeSplit' column
df = df.drop('objectToBeSplit', axis=1)

Approach 2:

# Create new columns C, D, E by splitting the values in the "objectToBeSplit" column
df[['C', 'D', 'E']] = pd.DataFrame(df['objectToBeSplit'].tolist()).applymap(float)

# Dropping the original 'objectToBeSplit' column
df = df.drop('objectToBeSplit', axis=1)

# Print the updated DataFrame
print(df)

答案2

得分: 1

以下是代码的中文翻译:

假设有一个set的系列:

out = (df.drop(columns='objectToBeSplit')
         .join(pd.DataFrame(df['objectToBeSplit'].tolist(),
                            columns=['C', 'D', 'E']))
      )

或者,如果你想要重新分配给相同的名称:

df = df.join(pd.DataFrame(df.pop('objectToBeSplit').tolist(),
                          columns=['C', 'D', 'E']))

输出:

   A  B      C      D      E
0  1  2  0.223  0.112  0.441
1  3  4  0.423  0.402  0.593

可重现的输入:

df = pd.DataFrame({'A': [1, 3],
                   'B': [2, 4],
                   'objectToBeSplit': [{0.112, 0.223, 0.441},
                                       {0.402, 0.423, 0.593}],
                   })
英文:

Assuming a Series of set:

out = (df.drop(columns='objectToBeSplit')
         .join(pd.DataFrame(df['objectToBeSplit'].tolist(),
                            columns=['C', 'D', 'E']))
      )

Or, if you want to reassign to the same name:

df = df.join(pd.DataFrame(df.pop('objectToBeSplit').tolist(),
                          columns=['C', 'D', 'E']))

Output:

   A  B      C      D      E
0  1  2  0.223  0.112  0.441
1  3  4  0.423  0.402  0.593

Reproducible input:

df = pd.DataFrame({'A': [1, 3],
                   'B': [2, 4],
                   'objectToBeSplit': [{0.112, 0.223, 0.441},
                                       {0.402, 0.423, 0.593}],
                   })

huangapple
  • 本文由 发表于 2023年5月13日 20:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定