在同一个Pandas数据帧列中查找值的所有其他实例

时间:2017-11-05 17:30:10

标签: python pandas

对于pandas dataframe列中的值,如何在同一列中查找该值的所有其他实例?

彼得的回答让我走上了正轨。这让我接近我需要的东西。它遍历列中的值,并为每个值获取索引号,它在同一列中找到相同的值。如果有更好的方法可以让我知道。

for i in range(0, len(df)):
    temp_a=df[df['a'] == df.iloc[i]['a']].index.tolist()

4 个答案:

答案 0 :(得分:0)

我们想说你想在第一栏找到1

a = 1 2 3
    4 5 6
    1 2 2

你可以这样做:

b = a.loc[:,0] #0 is column number
b[b==1] # 1 is the value to be found 

答案 1 :(得分:0)

   col1  col2
0     1     3
2     1     5

在此示例中,您需要列中值等于 1 的行。

将输出打印为:

print(df.loc[df['col1'] == 1].index.tolist())

现在,要获取行号:

[0, 2]

哪个会给你:

Starting Magento installation:
File permissions check...

Required extensions check...

Enabling Maintenance Mode...

Installing deployment configuration...

Installing database schema:
Schema creation/updates:
Module 'Magento_Store':
Installing schema... Upgrading schema... 

Module 'Magento_AdvancedPricingImportExport':

Module 'Magento_Directory':
Installing schema... 

Module 'Magento_Theme':
Installing schema... 

Module 'Magento_Backend':

Module 'Magento_Backup':

Module 'Magento_Eav':
Installing schema... Upgrading schema... 

Module 'Magento_Customer':
Installing schema... Upgrading schema... 

Module 'Magento_BundleImportExport':

Module 'Magento_CacheInvalidate':

Module 'Magento_AdminNotification':
Installing schema... 

可以找到更深入的解决方案:Select rows from a DataFrame based on values in a column in pandas 我还咨询了https://stackoverflow.com/a/46247791/5986661

答案 2 :(得分:0)

if (FC.Contains(Abreviated.ToString()))
        {
            MessageBox.Show("Match found");

        }
        else
        {
            MessageBox.Show("No Match");

        }

例如:

for v in df['col_name'].unique():
    print df[df['col_name'] == v]

答案 3 :(得分:0)

如果您只对列中每个值的索引感兴趣,还可以使用groupby方法:

df = pd.DataFrame(['a', 'b', 'a', 'b', 'c', 'c', 'd'], columns=['col1'])
df.groupby('col1').apply(lambda g: g.index.tolist())

col1
a    [0, 2]
b    [1, 3]
c    [4, 5]
d       [6]
dtype: object