简体   繁体   English

如何将列中的结果拆分为 python 中的两个单独列?

[英]How could I split the result in a column into two separate columns in python?

this the col that I need to split in python这是我需要在 python 中拆分的 col

在此处输入图像描述

I've tried this but it back with an error我已经尝试过了,但它返回错误

events["top_element_info"]= events["top_element_info"].str.split(",", n = 1, expand = True)

error错误

Assuming you want the ids as your first column and points as the second one, you can do the following:假设您希望将 ids 作为第一列并将点作为第二列,您可以执行以下操作:

df = pd.DataFrame({"top_element_info": [{'id': 123, 'points': 20}, {'id': 124, 'points': 30}, {'id': 125, 'points': 40}]})
for col in ['id', 'points']:
    df[col] = [x[col] for x in df['top_element_info']]

I am assuming you want to split the string into two and store it into two columns.我假设您想将字符串分成两部分并将其存储到两列中。 The issue is you are trying to split the string into two columns by assigning expand to True but trying to store it into a single column.问题是您试图通过将 expand 分配给 True 将字符串拆分为两列,但尝试将其存储到单个列中。

Try:尝试:

events["top_element_info_id"], events["top_element_info_points"] = events["top_element_info"].str.split(",", n = 1, expand = True)

after combining the solutions I've reached this结合解决方案后,我已经达到了这个

events["top_element_info"] = events["top_element_info"].apply(str)
events["top_element_info"].replace(regex=True,inplace=True,to_replace=r"'id':",value=r'')
events["top_element_info"].replace(regex=True,inplace=True,to_replace=r"'points': ",value=r'')
events["top_element_info"].replace(regex=True,inplace=True,to_replace=r"}",value=r'')
events["top_element_info"].replace(regex=True,inplace=True,to_replace=r"}",value=r'')

events[['top_element_info_id', 'top_element_info_points']] = events['top_element_info'].str.split(',', 2, expand=True)
events.head()

and it solved now thanks for all:)现在解决了,谢谢大家:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PYTHON - 如何根据分隔符将一列分成两列 - PYTHON - How do I separate a column into two columns based on a delimiter 查找一列的nlargest(2)并将结果拆分为单独的列 - Find nlargest(2) of a column and split the result into separate columns 如何在python中将一列拆分为两列? - How to split one column into two columns in python? 如何在 Python 中将列中的数据拆分为一些单独的列? - How to split data in a column into some separate columns in Python? 如何拆分混合数据列以分隔 python 中的 int 和 str 列 - how to split a mixed data column to separate int and str columns in python (Python)如何将具有日期和时间的列(例如UTC 2019-07-02 00:12:32)拆分为两个单独的列? - (Python) How can I split a column with both date and time (e.g. 2019-07-02 00:12:32 UTC) into two separate columns? 如何将串联的列名称拆分为单独的列? - How to split concatenated column name into separate columns? 我如何将这些数据分成两个单独的列表,分别为 python 中的 plot? - How would I split these data in to two separate lists to plot in python? 如何在python中将列分成3列 - How to separate column into 3 columns in python 如何使用 str.split 或正则表达式在 python 中将一列拆分为两列或多列? - How to split a column into two or multiple columns columns in python using either str.split or regex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM