1

Code is below

I am using python version 3.5

# train a random forest classifier
rf = RandomForestClassifier(n_estimators = 100, random_state = 42)
rf.fit(X_train, y_train)

# show feature importance
feature_importances_df = pd.DataFrame({"feature": features, "importance": rf.feature_importances_}).sort_values("importance", ascending = False)
feature_importances_df.head(20)

tried

feature_importances = {"feature": features, "importance": rf.feature_importances_}.sort_values("importance", ascending = False)
feature_importances_df = pd.DataFrame.from_dict(feature_importances, orient='index')

I was trying to learn sentimental analysis. got stuck with error

AttributeError: 'dict' object has no attribute 'sort_values'

1
  • move the sort_values to after the pd.DataFrame.from_dict Commented Dec 28, 2019 at 3:31

1 Answer 1

1

I am using Python 3.6.8 and pandas 0.25.1

try this

dictionary = {"a":[2,3,1,4],"b":["a","f","g","z"]}
dataFrame = pd.DataFrame.from_dict(dictionary)
dataFrame.sort_values('a',ascending=False,inplace = True)
dataFrame.reset_index(drop=True)

This will print

   a  b
0  4  z
1  3  f
2  2  a
3  1  g

Hope that it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.