Convert categorical data in pandas dataframe

First, to convert a Categorical column to its numerical codes, you can do this easier with: dataframe[‘c’].cat.codes.Further, it is possible to select automatically all columns with a certain dtype in a dataframe using select_dtypes. This way, you can apply above operation on multiple and automatically selected columns. First making an example dataframe: Then by using select_dtypes to select the … Read more

Python Error – int object has no attribute

As time = 3 is declared as an integer, time.time doesn’t have any sense since time is int variable (that isn’t a class but a primitive data type). I suppose that you expected to call time (module) writing time but, since you’re redefining it as an integer, this last definition shadows the time module Change time variable name to something else, like myTime Error messages are usefull, you should read … Read more

How to find char in string and get all the indexes?

This is because str.index(ch) will return the index where ch occurs the first time. Try: This will return a list of all indexes you need. P.S. Hugh’s answer shows a generator function (it makes a difference if the list of indexes can get large). This function can also be adjusted by changing [] to ().

How to make a histogram from a list of data

do you have any idea how to make 200 evenly spaced out bins, and have your program store the data in the appropriate bins? You can, for example, use NumPy’s arange for a fixed bin size (or Python’s standard range object), and NumPy’s linspace for evenly spaced bins. Here are 2 simple examples from my matplotlib gallery Fixed bin size … Read more

RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility

According to MAINT: silence Cython warnings about changes dtype/ufunc size. – numpy/numpy: These warnings are visible whenever you import scipy (or another package) that was compiled against an older numpy than is installed. and the checks are inserted by Cython (hence are present in any module compiled with it). Long story short, these warnings should be benign … Read more