简体   繁体   中英

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. 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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM