将熊猫df中的不同长度长度的元素拆分字符串

发布于 2025-01-21 21:01:20 字数 1093 浏览 0 评论 0原文

我有一个看起来像这个

IDhuman_id
1('苹果','2022-12-04','a5ted')
2('Bananas','2012-2-14')
3('2012-2-14) 的数据框','reda21','ss')
....

我想要一种“ Pythonic”的方法,使ID

ID IDhuman_idcol1col2 col2col3
1('apples','2022-12-04','a5ted')苹果2022-12-04A5TED
2('Bananas','2012-2-14')香蕉2022-12-04NP.NAN
3('2012-2-14','reda21','ss','SS')2012-2 -14reda21
import pandas as pd

df['a'], df['b'], df['c'] = df.human_id.str

我尝试给我错误的代码

valueerror:没有足够的值解开包装(预期2,获得1)python

如何将元组中的值拆分为列?

谢谢。

I have a dataframe that looks like this

idhuman_id
1('apples', '2022-12-04', 'a5ted')
2('bananas', '2012-2-14')
3('2012-2-14', 'reda21', 'ss')
....

I would like a "pythonic" way to have such output

idhuman_idcol1col2col3
1('apples', '2022-12-04', 'a5ted')apples2022-12-04a5ted
2('bananas', '2012-2-14')bananas2022-12-04np.NaN
3('2012-2-14', 'reda21', 'ss')2012-2-14reda21ss
import pandas as pd

df['a'], df['b'], df['c'] = df.human_id.str

The code I have tried give me error:

ValueError: not enough values to unpack (expected 2, got 1) Python

How can I split the values in tuple to be in columns?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云胡 2025-01-28 21:01:20

您可以这样做。它只会在找不到值的地方放置。然后,您可以将DF1附加到DF。

d = {'id': [1,2,3], 
     'human_id': ["('apples', '2022-12-04', 'a5ted')", 
                  "('bananas', '2012-2-14')",
                  "('2012-2-14', 'reda21', 'ss')"
                 ]}

df = pd.DataFrame(data=d)

list_human_id = tuple(list(df['human_id']))

newList = []
for val in listh:
    newList.append(eval(val))

df1 = pd.DataFrame(newList, columns=['col1', 'col2', 'col3'])

print(df1)

Output


        col1        col2   col3
0     apples  2022-12-04  a5ted
1    bananas   2012-2-14   None
2  2012-2-14      reda21     ss

You can do it this way. It will just put None in places where it couldn't find the values. You can then append the df1 to df.

d = {'id': [1,2,3], 
     'human_id': ["('apples', '2022-12-04', 'a5ted')", 
                  "('bananas', '2012-2-14')",
                  "('2012-2-14', 'reda21', 'ss')"
                 ]}

df = pd.DataFrame(data=d)

list_human_id = tuple(list(df['human_id']))

newList = []
for val in listh:
    newList.append(eval(val))

df1 = pd.DataFrame(newList, columns=['col1', 'col2', 'col3'])

print(df1)

Output


        col1        col2   col3
0     apples  2022-12-04  a5ted
1    bananas   2012-2-14   None
2  2012-2-14      reda21     ss

墨洒年华 2025-01-28 21:01:20

你可以做

out = df.join(pd.DataFrame(df.human_id.tolist(),index=df.index,columns=['a','b','c']))

You can do

out = df.join(pd.DataFrame(df.human_id.tolist(),index=df.index,columns=['a','b','c']))
岁吢 2025-01-28 21:01:20

列将使用元组长度创建动态,并使用相同的dataFrame

import pandas as pd

id = [1, 2, 3]
human_id = [('apples', '2022-12-04', 'a5ted')
            ,('bananas', '2012-2-14')
            , ('2012-2-14', 'reda21', 'ss')]

df = pd.DataFrame({'id': id, 'human_id': human_id})

print("*"*20,'Dataframe',"*"*20)
print(df.to_string())

print()

print("*"*20,'Split Data',"*"*20)

row = 0

for x in df['human_id']:

    col = 1

    for xx in x:

        #df['col'+str(z)] = str(xx)

        name_column = 'col'+str(col)
        df.loc[df.index[row], name_column] = str(xx)

        col+=1

    row+=1

print(df.to_string())

”在此处输入图像说明”

column will create dynamic with length of tuple and using same dataframe

import pandas as pd

id = [1, 2, 3]
human_id = [('apples', '2022-12-04', 'a5ted')
            ,('bananas', '2012-2-14')
            , ('2012-2-14', 'reda21', 'ss')]

df = pd.DataFrame({'id': id, 'human_id': human_id})

print("*"*20,'Dataframe',"*"*20)
print(df.to_string())

print()

print("*"*20,'Split Data',"*"*20)

row = 0

for x in df['human_id']:

    col = 1

    for xx in x:

        #df['col'+str(z)] = str(xx)

        name_column = 'col'+str(col)
        df.loc[df.index[row], name_column] = str(xx)

        col+=1

    row+=1

print(df.to_string())

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文