简体   繁体   English

从另一列创建列表列并仅显示 pandas dataframe 中的唯一值

[英]Create a column of list from another column and display only unique values in pandas dataframe

I am new to pandas, I am trying to use group by and create a list of in a new column.我是 pandas 的新手,我正在尝试使用 group by 并在新列中创建列表。 I have 3 columns in my Dataframe and I created a 4th column(New_List) to create a list from another column like below: using the below code:我的 Dataframe 中有 3 列,我创建了第 4 列(New_List)以从另一列创建列表,如下所示:使用以下代码:

new_df = df.join(pd.Series(df.groupby(by='NO_ACCOUNTS').apply(lambda x: list(x.Bucket)), name="list_of_b"), on='NO_ACCOUNTS') new_df = df.join(pd.Series(df.groupby(by='NO_ACCOUNTS').apply(lambda x: list(x.Bucket)), name="list_of_b"), on='NO_ACCOUNTS')

Account_Number   Bucket  Number_Transactions     New_List
   ABA            APP          155                 [APP]
   ABC            APP          1352                [APP]
   AAA            APP          90                  [API,APP]
   AAA            API          5                   [API,APP]

I am looking to get the desired output with 3 columns:我正在寻找具有 3 列的所需 output:

Account_Number     Number_Transactions     New_List
   ABA                      155                 [APP]
   ABC                      1352                [APP]
   AAA                      95                  [API,APP]

You can agg regate the two columns:您可以agg这两列:

out = (df.groupby("Account_Number", sort=False, as_index=False)
         .agg(Number_Transactions=("Number_Transactions", "sum"),
              New_List=("Bucket", list)))

which first groups by Account_Number while keeping their order with sort=False and not making it index with as_index=False , and then aggregates the Number_Transactions column with summation and appoints it to the same name columns and similarly, aggs the Bucket column with list and assigns it to New_List column in the output,首先按Account_Number分组,同时使用sort=False保持其顺序,而不是使用as_index=False使其索引,然后将Number_Transactions列与 summation 聚合并将其指定给相同名称的列,同样,将Bucket列与list聚合并分配它到New_List中的 New_List 列,

to get要得到

>>> out

  Account_Number  Number_Transactions    New_List
0            ABA                  155       [APP]
1            ABC                 1352       [APP]
2            AAA                   95  [APP, API]

暂无
暂无

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

相关问题 在 Pandas 数据框中创建一个新的列表列,其中包含来自另一列的唯一值 - Create a new column of lists in Pandas dataframe with unique values from another column 如何根据 pandas dataframe 中另一列的多个值在一列中创建值列表? - How do I create a list of values in a column from several values from another column in a pandas dataframe? Pandas:将 dataframe 的列从列表转换为字符串,并且字符串只有列表的唯一值 - Pandas: Convert column of dataframe from list to string and the string to have only unique values of list 如何创建从列中获取的唯一值的熊猫数据框,没有重复项 - How to create a pandas dataframe of unique values fetched from column with no duplicates 在 pandas 中的 dataframe 中创建列的唯一值字典 - Create a dictionary of unique values of a column in a dataframe in pandas 使用Pandas中的列的唯一值创建一个DataFrame - Create a DataFrame with unique values of a Column in Pandas 从 dict 创建 dataframe pandas ,其中值是元组列表,每个列名都是唯一的 - Create dataframe pandas from dict where values are list of tuples and each column name is unique Pandas Dataframe:从另一列中唯一值最多的列中查找唯一值 - Pandas Dataframe: Find unique value from one column which has the largest number of unique values in another column 从Pandas Dataframe中找到列中的唯一值,然后查看这些值在另一列中是否具有相同的值 - From Pandas Dataframe find unique values in column and see if those values have the same values in another column Python Pandas:仅当列值唯一时,才将数据框追加到另一个数据框 - Python Pandas: Append Dataframe To Another Dataframe Only If Column Value is Unique
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM