将 pandas 数据框转换为 bool 值
我正在尝试编写一段代码来检查 pandas 系列的 Unix
列中是否有重复项。它应该为 DataSet
返回 bool
值 True
,因为 130
重复两次并且 对于
。我怎样才能得到下面的预期输出?DataSet2
为 False
import pandas as pd
DataSet = pd.DataFrame({'Unix':[130, 140, 150, 130],
'Value':[11,2,3,4]})
DataSet2 = pd.DataFrame({'Unix':[130, 140, 150, 160],
'Value':[11,2,3,4]})
print(DataSet.duplicated(subset=['Unix']).bool())
print(DataSet2.duplicated(subset=['Unix']).bool())
预期输出:
True
False
I am trying to write a code that checks if there are any duplicates in the Unix
column in the pandas series. It is supposed to return a bool
value of True
for DataSet
since 130
is repeated twice and a False
for DataSet2
. How would I be able to get the expected output below?
import pandas as pd
DataSet = pd.DataFrame({'Unix':[130, 140, 150, 130],
'Value':[11,2,3,4]})
DataSet2 = pd.DataFrame({'Unix':[130, 140, 150, 160],
'Value':[11,2,3,4]})
print(DataSet.duplicated(subset=['Unix']).bool())
print(DataSet2.duplicated(subset=['Unix']).bool())
Expected Output:
True
False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需要调用
.any()
,如果集合中有任何真值作为参数传递给它,它就会返回True
(相反,.all( )
仅当给定集合中的所有值都为真时才返回True
):但是,我认为您可能不小心为
DataSet2
复制了错误的数据,因为它是与DataSet1
相同并且False
答案没有意义。请注意,您还可以使用
.is_unique
,它更适合您的用例:作为参考,
.any()
和.all()< /code> 函数是一种便捷方法,逻辑上相当于分别对集合中的所有项目使用
or
和and
。You just need to call
.any()
which returnsTrue
if there are any truthy values in the collection passed to it as an argument (conversely,.all()
returnsTrue
only if all values in a given collection are truthy):However, I think you may have accidentally copied the wrong data for
DataSet2
as it is identical toDataSet1
and aFalse
answer wouldn't make sense.Note that you can also use
.is_unique
, which is more appropriate for your use-case:For reference, the
.any()
and.all()
functions are convenience methods that are logically equivalent to usingor
andand
, respectively, over all the items in a collection.使用
.any()
而不是.bool()
:输出:(
请注意,给定的
DataSet
和DataSet1
问题中的内容是相同的,因此两者都是True
,我认为这只是一个拼写错误,并不反映您的实际数据。)Use
.any()
instead of.bool()
:Output:
(Note that
DataSet
andDataSet1
as given in the question are identical, thusTrue
for both. I assume this is just a typo and does not reflect your actual data.)你可以得到 DataSet True 以下代码
我不知道为什么 DataSet2 是 False
you can get DataSet True following code
i don know why DataSet2 is False