简体   繁体   English

如何在熊猫数据框中多次检查列中的值是否存在于同一列中

[英]how to check if a value in a column exists in same column multiple times in a pandas dataframe

My problem statement is that I want to check if a value in a column in a pandas data frame occurs for multiple times or not.我的问题陈述是我想检查熊猫数据框中的列中的值是否多次出现。 How can that be done?怎么可能呢? I am thinking of applying two for loops and something like this to search for multiple occurrences.我正在考虑应用两个 for 循环和类似的东西来搜索多个事件。 But this is showing an error:但这显示了一个错误:

for i in range(df_containing_nft.size):
    Text_to_search=df_containing_nft.iat[i,0]
    for j in range(i+1, df_containing_nft.size):
        if (df_containing_nft.iat[j,0]==Text_to_search):
            print(Text_to_search)

The error is the following:错误如下:

IndexError                                Traceback (most recent call last)
C:\Users\KESABC~1\AppData\Local\Temp/ipykernel_17020/3015476165.py in <module>
      2     Text_to_search=df_containing_nft.iat[i,0]
      3     for j in range(i, df_containing_nft.size):
----> 4         if (df_containing_nft.iat[j,0]==Text_to_search):
      5             print(Text_to_search)

~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   2220 
   2221         key = self._convert_key(key)
-> 2222         return self.obj._get_value(*key, takeable=self._takeable)
   2223 
   2224     def __setitem__(self, key, value):

~\anaconda3\lib\site-packages\pandas\core\frame.py in _get_value(self, index, col, takeable)
   3564         if takeable:
   3565             series = self._ixs(col, axis=1)
-> 3566             return series._values[index]
   3567 
   3568         series = self._get_item_cache(col)

IndexError: index 91 is out of bounds for axis 0 with size 91

df.size returns the number of value in the whole DataFrame. df.size返回整个 DataFrame 中值的数量。 Since you only want to loop through index, you may want由于您只想遍历索引,因此您可能想要

for i in range(df_containing_nft.shape[0]): # or len(df_containing_nft)
    Text_to_search=df_containing_nft.iat[i,0]
    for j in range(i+1, df_containing_nft.shape[0]):
        if (df_containing_nft.iat[j,0]==Text_to_search):
            print(Text_to_search)

I guess what you want to do is我想你想做的是

df_containing_nft[df_containing_nft['col'].duplicated(), 'col'].tolist()

暂无
暂无

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

相关问题 如何检查pandas数据帧中是否存在具有特定列值的行 - How to check if there exists a row with a certain column value in pandas dataframe 如何检查熊猫数据帧列中的子字符串是否存在于同一数据帧中另一列的子字符串中? - How to check if a substring in a pandas dataframe column exists in a substring of another column in the same dataframe? 如何检查 DataFrame 列中是否存在元组值 - How to check if a tuple value exists in a DataFrame column 如何在另一个数据帧列pandas中检查一个数据帧的列值多少次? - how to check column value of one data frame how many times in another dataframe column pandas? Pandas DataFrame检查列值是否存在列值 - Pandas DataFrame check if column value exists in a group of columns Pandas - 检查列中的值是否存在于 MultiIndex 数据帧的任何索引中 - Pandas - Check if value from a column exists in any index of a MultiIndex dataframe 将同一行从 pandas dataframe 多次添加到新行,每次更改特定列中的值 - Add the same row multiple times from a pandas dataframe to a new one, each time altering a value in a specific column 如何检查多个列表中的任何一个中是否存在 DataFrame 列值,如果不存在,则填充另一列? - How do I check if a DataFrame column value exists in any of multiple lists, and if not, fill another column? 如何多次替换Pandas Column中的值? - How to replace a value in Pandas Column multiple times? 如何通过(索引,列)签入 Pandas DataFrame 条目 - how to check in a pandas DataFrame entry exists by (index, column)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM