简体   繁体   English

如何根据 pandas dataframe 中另一列的多个值在一列中创建值列表?

[英]How do I create a list of values in a column from several values from another column in a pandas dataframe?

I have a dataframe with these values:我有一个 dataframe 具有这些值:

filename, keyword, page
A, red, 1
A, red, 2
A, green, 1
B, red, 1
B, green, 1
C, green, 2

How can I transform this to the following format?如何将其转换为以下格式?

filename, keywords, pages
A, [red, green], [1,2]
B, [red, green], [1]
C, [green], [2]

Is there an easy way to do this in Pandas?在 Pandas 中有没有简单的方法可以做到这一点? If a list isn't allowed as a cell value, is there another datatype that I could use that Pandas would allow?如果不允许将列表作为单元格值,是否有我可以使用 Pandas 允许的另一种数据类型? Or an alternative to a Pandas dataframe that I could store this in and then save it to a csv?或者我可以将其存储在 Pandas dataframe 中,然后将其保存到 csv 中?

you could use df.groupby(["filename"])['keyword','page'].agg(set)你可以使用 df.groupby(["filename"])['keyword','page'].agg(set)

keyword page
filename        
A   {green, red}    {1, 2}
B   {green, red}    {1}
C   {green} {2}

( PS: updated based on Ch3steR answers, i was only using list instead of set ( PS:根据 Ch3steR 答案更新,我只使用列表而不是集合

暂无
暂无

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

相关问题 Pandas DataFrame:如何从另一列的数值中创建数值? - Pandas DataFrame: How do I create numerical values out of numerical values from another column? 如何从另一列的所有值创建新的列名并按 pandas dataframe 中的另一列创建新列名? - how to create new column names from another column all values and agg by another column in pandas dataframe? 如果使用熊猫在另一个数据帧中不存在列值,如何将它们从一个数据帧合并到另一个数据帧 - How do I merge column values from one dataframe to another if they are not present in another using pandas 如何基于另一列的值在pandas dataframe列中创建新值 - How to create new values in a pandas dataframe column based on values from another column 给定 pandas DataFrame 中一列中的值列表,如何从同一行中的另一列中获取 output 中的值? - Given a list of values in a column in pandas DataFrame, how to output values from another column in the same rows? 从另一列创建列表列并仅显示 pandas dataframe 中的唯一值 - Create a column of list from another column and display only unique values in pandas dataframe 如何从另一个 dataframe 创建一个 dataframe,每个值列只有最后一个非负值? - How do I create a dataframe from another dataframe with only the last non negative values for each value column? 如何使用另一个 dataframe 中的值在 dataframe 中创建新列? - How do I create a new column in a dataframe using values from another dataframe? 如何从字典列表中的特定值在单独的 Dataframe 列中创建列表? - How do I create a list in a separate Dataframe column from specific values from within a list of dictionaries? 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM