[英]Pandas reset_index(drop=True) not working correctly with groupby
I have a pandas Datarame as below:我有一个 pandas Datarame 如下:
data = {
'user_id': [1, 1, 1, 1, 1, 2],
'tag_id': [700, 700, 700, 701, 701, 700],
'score': [1, 0.9, 0.8, 0.7, 1, 0.6]
}
df = pd.DataFrame(data)
>>> df
user_id tag_id score
0 1 700 1.0
1 1 700 0.9
2 1 700 0.8
3 1 701 0.7
4 1 701 1.0
5 2 700 0.6
Then I applied a groupby
operation on my DataFrame as below, which basically sums scores in a group, but it doesn't remove indexes :然后我在我的 DataFrame 上应用了
groupby
操作,如下所示,它基本上是对一组中的分数求和,但它不会删除索引:
df = df.groupby(['user_id', 'tag_id'], as_index=False).sum().reset_index(drop=True)
>>> df
user_id tag_id score
0 1 700 2.7
1 1 701 1.7
2 2 700 0.6
I also tried removing as_index
parameter, but it removes other columns, and still the indexes are not removed:我也尝试删除
as_index
参数,但它删除了其他列,并且仍然没有删除索引:
df = df.groupby(['user_id', 'tag_id']).sum().reset_index(drop=True)
>>> df
score
0 2.7
1 1.7
2 0.6
Any ideas on how I can remove these indexes ?关于如何删除这些索引的任何想法?
In the comments OP expressed his goal在评论中,OP表达了他的目标
I want to store this DataFrame without indexes with df.to_sql command into a database.
我想用 df.to_sql 命令将这个没有索引的 DataFrame 存储到数据库中。 My main problem is on how can I ignore these indexes
我的主要问题是如何忽略这些索引
In order to do that, passing index=false
in pandas.DataFrame.to_sql
solves the problem为此,在
pandas.DataFrame.to_sql
中传递index=false
可以解决问题
df.to_sql(index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.