gpt4 book ai didi

python - 在 Pandas 中将两个数据合并在一起

转载 作者:行者123 更新时间:2023-11-30 23:04:33 25 4
gpt4 key购买 nike

我正在尝试将 pandas 中的数据连接在一起,但它似乎对我来说效果不佳。

我有一些数据想要转换为数字,并且我能够做到这一点。然后我想让它重新加入数据集。

原始数据如下:

 CallDate            Agent          Group     Direction
0 2015-09-01 Adam Billing Inbound
1 2015-09-01 Nathaniel Billing Outbound
2 2015-09-01 Jessica Claims Inbound
3 2015-09-01 Tom Billing Outbound
4 2015-09-01 Jane CCS Inbound

这是我将组转换为数字的代码

data['Group']=data['Group'].astype(str)
data.Group=data['Group'].apply(lambda x:len(x))

这很有效,给了我我想要的东西 0 1 1 1 2 13 3 1 4 6

然后我尝试将其合并回组(基本上我想知道每个名称/号码对应的内容)

y=pd.concat([data,data.Group], ignore_index=True)
y [:5]

但是结果和原数据库是一样的

是否有一些明显的我遗漏的东西或者我没有想到的更简单的解决方法。

最佳答案

pd.concat() 是连接两个DataFrame。我认为您正在尝试连接 DataFrame 中的两列。

data
Out[42]:
CallDate Agent Group Direction
0 2015-09-01 Adam Billing Inbound
1 2015-09-01 Nathaniel Billing Outbound
2 2015-09-01 Jessica Claims Inbound
3 2015-09-01 Tom Billing Outbound
4 2015-09-01 Jane CCS Inbound

data.Group = data.Group + data.Group.apply(lambda x:" / "+str(len(x)))

data
Out[44]:
CallDate Agent Group Direction
0 2015-09-01 Adam Billing / 7 Inbound
1 2015-09-01 Nathaniel Billing / 7 Outbound
2 2015-09-01 Jessica Claims / 6 Inbound
3 2015-09-01 Tom Billing / 7 Outbound
4 2015-09-01 Jane CCS / 3 Inbound

您可以在 pandas concat API documentation 中找到更多详细信息

更新新专栏,

data['Group_1'] = data.Group + data.Group.apply(lambda x:" / "+str(len(x)))

data
Out[56]:
CallDate Agent Group Direction Group_1
0 2015-09-01 Adam Billing Inbound Billing / 7
1 2015-09-01 Nathaniel Billing Outbound Billing / 7
2 2015-09-01 Jessica Claims Inbound Claims / 6
3 2015-09-01 Tom Billing Outbound Billing / 7
4 2015-09-01 Jane CCS Inbound CCS / 3

关于python - 在 Pandas 中将两个数据合并在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33636213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com