简体   繁体   English

我在熊猫数据框中有两列。 一列具有相同的值(id),另一列中具有对应的值的票证数

[英]I have two columns in a pandas dataframe. One column has some same values (id) and corresponding values in another column has Number of tickets

   id         n_tickets
0  1586391          2
1   640             2
2   640             1
3  1181593          2
4   964842          1
5    780            1
6    780            1  

I have the above data frame. 我有上面的数据框。 As you can see that in id column 640 and 780 have occurred two times. 如您所见,在id列640和780中已发生两次。 I want that just a single occurrence remains but there corresponding values in column n_tickets gets added up. 我希望只保留一次,但要在n_tickets列中添加相应的值。 My final dataframe should look like this: 我的最终数据框应如下所示:

     id         n_tickets
0  1586391          2
1   640             3
2  1181593          2
3   964842          1
4    780            2 

I am using the code: df_tickets.groupby(['id','n_tickets']).sum() but I am gettting the error: 我正在使用代码: df_tickets.groupby(['id','n_tickets']).sum()但是我得到了以下错误:

Empty DataFrame
Columns: []
Index: []

when i use the below code: 当我使用以下代码时:

df_tickets.groupby('id',sort=False).sum().reset_index()

print(df_tickets.loc[df_tickets['id'] == 780])

I get this: 我得到这个:

        id     n_tickets
425166  780          1
985855  780          1

Instead I should get: 相反,我应该得到:

           id    n_tickets
   425166  780      2

Try using: 尝试使用:

df_tickets = df_tickets.groupby(df_tickets['id']).sum()

This should work. 这应该工作。

You only need to group by 'id': 您只需要按“ id”分组:

df.groupby('id',sort=False).sum().reset_index()
Out[60]: 
        id  n_tickets
0  1586391          2
1      640          3
2  1181593          2
3   964842          1
4      780          2

when i use the above code: 当我使用上面的代码时:

df_tickets.groupby('id',sort=False).sum().reset_index()

    print(df_tickets.loc[df_tickets['id'] == 780])

I get this: 我得到这个:

         id     n_tickets
425166  780          1
985855  780          1

Instead I should get: 相反,我应该得到:

    id    n_tickets
 425166  780      2

暂无
暂无

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

相关问题 将具有 df 的两列值的两个数据框与另一个数据框的单列值连接起来。 基于某些条件? - Join two data frame with two columns values of a df with a single column values of another dataframe. based on some conditions? Pandas DataFrame。 聚合列取决于另一列中的值 - Pandas DataFrame. Aggregate column in depends of values in another column Pandas Dataframe:从另一列中唯一值最多的列中查找唯一值 - Pandas Dataframe: Find unique value from one column which has the largest number of unique values in another column 如果 dataframe 列中的单元格在另一列中具有相同的对应值,如何将它们连接到列表中??[熊猫] - How to concatenate cells in dataframe column into lists if they have same corresponding values in another column??[Pandas] 我有数据框。 我需要创建一个以行为键的字典,以“True”作为字典值的列 - I have dataframe. I need to create a dictionary with row as the key and columns which has 'True' as the values of the dictionary 检查 pandas dataframe 列(将列表作为值)是否具有另一个列表的一个元素 - Checking if a pandas dataframe column(that has lists as values) has one element of another list 如果其他两个列在Pandas中具有匹配的值,如何用另一个数据框的值填充空列的值? - How to fill empty column values with another dataframe's value if two other columns have matching values in Pandas? 当一列在 pandas 中有两个变量值时,如何熔化 dataframe? - How to melt dataframe when a column has two values for a variable in pandas? 检查pandas DataFrame中的两行是否具有相同的一组值,无论列顺序如何 - Check if two rows in pandas DataFrame has same set of values regard & regardless of column order 在两列上对Pandas Dataframe进行排序,其中一列值按顺序重复 - Sort Pandas Dataframe on two columns with one column values repeating in sequence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM