enumerate pandas code example

Example 1: enumerate in python

languages = ['Python', 'C', 'C++', 'C#', 'Java']

#Bad way
i = 0 #counter variable
for language in languages:
    print(i, language)
    i+=1

#Good Way
for i, language in enumerate(languages):
    print(i, language)

Example 2: enumerate

categorical_features_new=[features for features in df.columns if df[features].dtypes=='object']
for feature in categorical_features_new:
    labels_sorted=df[feature].value_counts().sort_values().index
   
    labels_ordered={k:i for i,k in enumerate(labels_sorted,0)}
    
    df[feature]=df[feature].map(labels_ordered)

Tags:

C Example