【问题标题】:Merging two data together in Pandas在 Pandas 中将两个数据合并在一起
【发布时间】:2016-02-11 16:44:54
【问题描述】:

我正在尝试在 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]

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

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

【问题讨论】:

  • 同时发布其他数据集
  • 正如 WoodChopper 所说,您的代码不适合您的“原始数据”,它访问 AssignedWorkGroup 但这不在您显示的数据中。还包括为您打印这些数字(0 1 1 等)的代码,以及您期望的代码的完整输出。
  • @sgvd 抱歉忘记清理标签了
  • @user3120266 你想连接Billing + len(Billing)吗?
  • @WoodChopper 实际上不,它将是一个单独的列。所以组仍然存在,但随后是一个带有数字的单独列

标签: python pandas merge concatenation dataframe


【解决方案1】:

pd.concat() 是连接两个DataFrame。我认为您正在尝试连接 a 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

【讨论】:

    【解决方案2】:

    您可以使用cat 函数连接pandas 中的两个系列,查看Documentation 此处的cat 函数。

    您还可以使用len 函数df.Group.str.len() 轻松获取任何单词中的字符数

    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
    

    【讨论】:

    • 很高兴看到列表理解:)
    • 我顺便把它删了,这看起来好多了:)
    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 2021-10-13
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多