zara
zara

Reputation: 1098

How to find the column number of those columns with single value in Python?

I want to find the column number of columns with single value: here is my dataframe:

Cow_ID,Collar_label,BCS,Age_weeks,Height_cm,Weight_kg
cow1,aq5t,90,14,90,120
cow2,aq4r,92,14,92,118
cow3,aq2f,87,14,87,120
cow4,aq7u,81,14,81,118
cow5,aq9p,93,14,93,120
cow6,aq4m,89,14,89,120
cow7,aq1v,86,14,86,118
cow8,aq2c,92,14,92,120
cow9,aq5t,89,14,89,120
cow10,aq5x,88,14,88,118

and here is my code by which I can get the column name of the ones with single value:

df = read_csv(filename, header=0)
print("Data frame size: ", df.shape)
print("Data frame : ", df)

# get the number of unique values for each column
counts = df.nunique()


print("number of unique values within each column:")
print(counts)
 

print("column with single value: ", counts[counts ==1])

but I need column number to pop up automatically instead of column name. Any suggestion please?

Upvotes: 0

Views: 131

Answers (2)

Umar.H
Umar.H

Reputation: 23099

if you want the index of the column then create a dictionary with a key, value pair, the key being the index and the value being the column name.

enum = dict(enumerate(df.columns))

column_nums = {v : k for k,v in enum.items() if v in counts[counts ==1].index}

{'Age_weeks': 3}

print(enum)

{0: 'Cow_ID',
 1: 'Collar_label',
 2: 'BCS',
 3: 'Age_weeks',
 4: 'Height_cm',
 5: 'Weight_kg'}

Upvotes: 0

Amir P
Amir P

Reputation: 151

Get the index of the series with single values:

counts[counts ==1].index[0]
>> 'Age_weeks'

A conditional gives a list of True/False matching to column numbers:

df.columns == 'Age_weeks'
>> array([False, False, False,  True, False, False])

And use np.where to get the index of True item, which equals to your column number:

np.where(df.columns == counts[counts ==1].index[0])
>>(array([3]),)

To get the number:

np.where(df.columns == counts[counts ==1].index[0])[0][0]
>> 3

Upvotes: 1

Related Questions