Using BeautifulSoup to search HTML for string

The following line is looking for the exact NavigableString ‘Python’: Note that the following NavigableString is found: Note this behaviour: So your regexp is looking for an occurrence of ‘Python’ not the exact match to the NavigableString ‘Python’.

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 sort 2d array by row in python?

Python, per se, has no “2d array” — it has (1d) lists as built-ins, and (1d) arrays in standard library module array. There are third-party libraries such as numpy which do provide Python-usable multi-dimensional arrays, but of course you’d be mentioning such third party libraries if you were using some of them, rather than just saying “in Python”, right?-) … Read more

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