简体   繁体   English

Pandas 创建新列,其中来自另一列的所有条目对应于唯一值

[英]Pandas create new column with all the entries from another column corresponding to a unique value

I am sorry if the question is not clear enough.如果问题不够清楚,我很抱歉。 Say I have this dataframe:假设我有这个 dataframe:

timestamp source dest size

1          a      b     5
1          c      d     6
2          c      e     7
2          d      a     8

From this dataframe I want something like this:从这个 dataframe 我想要这样的东西:

timestamp      link        size
 1             a b c d     5 6
 2             c e d a     7 8

How can I achieve this?我怎样才能做到这一点?

Thank you谢谢

This is a pivot with a couple added steps since you want to pivot on two columns independently of each other.这是一个带有几个附加步骤的 pivot,因为您希望 pivot 在两列上彼此独立。

u = df.melt('timestamp')
m = u['variable'].isin(['source', 'dest'])

u.loc[m, 'variable'] = 'link'

u.pivot_table(
  'value', 'timestamp', 'variable', aggfunc=list)

variable           link    size
timestamp
1          [a, c, b, d]  [5, 6]
2          [c, d, e, a]  [7, 8]

An alternative using rename first首先使用rename的替代方法

d = dict(source='link', dest='link')

df.rename(columns=d).melt('timestamp').pivot_table(
  'value', 'timestamp', 'variable', aggfunc=list)

variable           link    size
timestamp
1          [a, c, b, d]  [5, 6]
2          [c, d, e, a]  [7, 8]

You can also use the groupby method of pandas dataframe.您也可以使用 pandas dataframe 的groupby方法。 Make sure that you size column contains strings.确保您的size列包含字符串。

df['link'] = df['source'] + ' ' + df['dest']
df = df.drop(['source', 'dest'], axis = 1)
newDf = df.groupby('timestamp').agg(lambda col: ' '.join(col))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多选项卡 Excel 工作表,1 列中的唯一条目,以另一列中的数据作为名称创建新文件,全部带有标题 - Multi tab Excel sheets, unique entries in 1 column, create new file with data from another column as the name, all with headers Pandas:按组创建包含另一列中最大值的对应值的新列 - Pandas: create new column containing the corresponding value of the maximum in another column by group 对于一列的所有唯一“单词”,从另一列中找到唯一的单元格,然后在python中平均另一列的对应值 - For all the unique 'words" of a column, the find unique cells from another column , then average corresponding values of another column in python 熊猫-将所有列中的特定值替换为另一列中的对应值 - Pandas - Replacing a specific value in all columns with the corresponding value in another column Python Pandas 旋转:如何在第一列中分组并为第二列中的每个唯一值创建一个新列 - Python Pandas pivoting: how to group in the first column and create a new column for each unique value from the second column 如何从另一列的所有值创建新的列名并按 pandas dataframe 中的另一列创建新列名? - how to create new column names from another column all values and agg by another column in pandas dataframe? 在 Pandas 数据框中创建一个新的列表列,其中包含来自另一列的唯一值 - Create a new column of lists in Pandas dataframe with unique values from another column 在 pandas 中的另一个值中创建一个新列 - Create a new column with value in another in pandas 将值从pandas中的另一列更改为相应的值 - change value to corresponding value from another column in pandas 根据一列的条件和熊猫中另一列的值创建新列 - Create new column based on condition from one column and the value from another column in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM