python - Creating a new column with concatenated values from another column
问题描述
I am trying to create a new column in this data frame. The data set has multiple records for each PERSON because each record is a different account. The new column values should be a combination of the values for each PERSON in the TYPE column. For example, if John Doe has four accounts the value next to his nae in the new column should be a concatenation of the values in TYPE. An example of the final data frame is below. Thanks in advance.
解决方案
You can do this in two lines (first code, then explanation):
Code:
in: name_types = df.pivot_table(index='Name', values='AccountType', aggfunc=set)
out:
AccountType
Name
Jane Doe {D}
John Doe {L, W, D}
Larry Wild {L, D}
Patti Shortcake {L, W}
in: df['ClientType'] = df['Name'].apply(lambda x: name_types.loc[x]['AccountType'])
Explanation:
The pivot table gets all the AccountTypes for each individual name, and removes all duplicates using the 'set' aggregate function.
The apply function then iterates through each 'Name' in the main data frame, looks up the AccountType associated with each in name_typed, and adds it to the new column ClientType in the main dataframe.
And you're done!
Addendum: If you need the column to be a string instead of a set, use:
in: def to_string(the_set):
string = ''
for item in the_set:
string += item
return string
in: df['ClientType'] = df['ClientType'].apply(to_string)
in: df.head()
out:
Name AccountType ClientType
0 Jane Doe D D
1 John Doe D LDW
2 John Doe D LDW
3 John Doe L LDW
4 John Doe D LDW
推荐阅读
- javascript - 如何访问 edgeHTML 17 中发布的精确触摸板指针事件
- javascript - 我的第一个 ExtJs 6 应用程序出错。Ext.app 未定义
- r - 如何增加ggplot2中分组条之间的空间?
- google-cloud-platform - 超过 Google Cloud Identity 免费用户限制
- python - 是否有另一种方法(更有效的方法)来编写代码,以便它可以运行得更快?
- elasticsearch - 查找 elasticsearch _score
- html - 如何在标签内对齐图像?
- c# - 在智能感知中显示 C# 参考文档
- laravel - 方法 Illuminate\Database\Query\Builder::onlyTrashed 不存在
- node.js - TypeError:userDetails.findAll 不是函数