Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Pandas中将两个数据合并在一起_Python_Pandas_Merge_Concatenation_Dataframe - Fatal编程技术网

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

Python 在Pandas中将两个数据合并在一起,python,pandas,merge,concatenation,dataframe,Python,Pandas,Merge,Concatenation,Dataframe,我正在尝试将熊猫中的数据连接在一起,但对我来说似乎效果不太好 我有一些数据,我想转换成一个数字,我能够做到这一点。然后我想让它重新加入数据集 以下是原始数据的外观: CallDate Agent Group Direction 0 2015-09-01 Adam Billing Inbound 1 2015-09-01 Nathaniel Billing Outbound

我正在尝试将熊猫中的数据连接在一起,但对我来说似乎效果不太好

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

以下是原始数据的外观:

 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

然后我尝试将其合并回组中(基本上我想知道每个名称/编号对应的是什么)

但结果与原始数据库相同

是否有一些明显的我遗漏的东西,或是我没有想到的更容易的工作

pd.concat()
是连接两个
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中找到更多详细信息

更新新列

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

您可以使用
cat
功能连接
pandas
中的两个系列,请检查此处的
cat
功能

使用
len
函数
df.Group.str.len()


张贴另一个数据集alsoAs WoodChopper说,你的代码不符合你的“原始数据”,它访问
AssignedWorkGroup
,但你显示的数据中没有。还包括为您打印这些数字(0 1 1等)的代码,以及您希望从代码中获得的完整输出。@sgvd抱歉忘了清理一下标签@user3120266是否要连接
计费
+
len(计费)
?@WoodChopper实际上不,它将是一个单独的列。所以这个组仍然存在,但是有一个单独的列,上面有数字,可以查看列表理解:)顺便说一句,我把它删除了,看起来好多了:)
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
df['Group'] = df.Group.str.cat(df.Group.str.len().astype(str), sep=' => ')
df
Out[42]:
CallDate    Agent          Group         Direction
2015-09-01  Adam          Billing => 7   Inbound
2015-09-01  Nathaniel     Billing => 7   Outbound
2015-09-01  Jessica       Claims => 6    Inbound
2015-09-01  Tom           Billing => 7   Outbound
2015-09-01  Jane          CCS => 3       Inbound