我有一个有很多tweet的数据帧,我想删除重复的。tweets存储在fh1.df['tweets']中。我计算不重复的数量。j重复的数量。在else语句中,我删除了重复的行。在“如果我做一个新的列表”tweetschecklist“里,我把所有好的tweets放进去
好吧,如果我做I+j,我就成了原创微博的数量。所以这很好。但在else中,我不知道为什么,他删除了很多行,因为在for循环(1/10)之后,我的数据帧的形状要小得多
“fh1.df=fh1.df[fh1.df.Tweets!=当前[推特] “行删除到多行
tweetChecklist = []
for current_tweet in fh1.df['Tweets']:
if current_tweet not in tweetChecklist:
i = i + 1
tweetChecklist.append(current_tweet)
else:
j = j + 1
fh1.df = fh1.df[fh1.df.Tweets != current_tweet]
fh1.df['Tweets'] = pd.Series(tweetChecklist)
在
pandas
中,通常有一种比用for
循环遍历数据帧更好的方法在这种情况下,您真正想要的是将相等的tweet分组在一起,只保留第一条tweet。这可以通过^{} 实现:
更妙的是,甚至有一个显式函数^{} ,它的速度大约是它的两倍:
注
Graipher的解决方案告诉您如何生成唯一的数据帧。我的答案告诉您为什么当前操作会删除太多行(根据您的问题)
尾注
当您输入“else”语句来删除重复的tweet时,您将删除具有指定tweet的所有行。让我们演示一下:
这意味着什么:
在您的方法中(假设您希望从“A”而不是“Tweets”中删除重复项),您将得到(即,只有不唯一的行)
如果你只是想让这个独特,实现Graipher的建议。如果要计算您有多少个副本,可以执行以下操作:
相关问题 更多 >
编程相关推荐