将熊猫df中的不同长度长度的元素拆分字符串
我有一个看起来像这个
ID | human_id |
---|---|
1 | ('苹果','2022-12-04','a5ted') |
2 | ('Bananas','2012-2-14') |
3 | ('2012-2-14) 的数据框','reda21','ss') |
.. | .. |
我想要一种“ Pythonic”的方法,使ID
ID ID | human_id | col1 | col2 col2 | col3 |
---|---|---|---|---|
1 | ('apples','2022-12-04','a5ted') | 苹果 | 2022-12-04 | A5TED |
2 | ('Bananas','2012-2-14') | 香蕉 | 2022-12-04 | NP.NAN |
3 | ('2012-2-14','reda21','ss','SS') | 2012-2 -14 | reda21 | : |
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
id | human_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
id | human_id | col1 | col2 | col3 |
---|---|---|---|---|
1 | ('apples', '2022-12-04', 'a5ted') | apples | 2022-12-04 | a5ted |
2 | ('bananas', '2012-2-14') | bananas | 2022-12-04 | np.NaN |
3 | ('2012-2-14', 'reda21', 'ss') | 2012-2-14 | reda21 | ss |
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做。它只会在找不到值的地方放置。然后,您可以将DF1附加到DF。
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.
你可以做
You can do
列将使用元组长度创建动态,并使用相同的dataFrame
column will create dynamic with length of tuple and using same dataframe