DataFrame constructor not properly called! error

You are providing a string representation of a dict to the DataFrame constructor, and not a dict itself. So this is the reason you get that error. So if you want to use your code, you could do: But better would be to not create the string in the first place, but directly putting it in a … Read more

How to parse XML and count instances of a particular node attribute?

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed — the ease of programming part depends on the API, which ElementTree defines. First build an Element instance root from the XML, e.g. with the XML function, or by parsing … Read more

What does numpy.random.seed(0) do?

np.random.seed(0) makes the random numbers predictable With the seed reset (every time), the same set of numbers will appear every time. If the random seed is not reset, different numbers appear with every invocation: (pseudo-)random numbers work by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The … Read more

How can I remove a key from a Python dictionary?

To delete a key regardless of whether it is in the dictionary, use the two-argument form of dict.pop(): This will return my_dict[key] if key exists in the dictionary, and None otherwise. If the second parameter is not specified (i.e. my_dict.pop(‘key’)) and key does not exist, a KeyError is raised. To delete a key that is guaranteed to exist, you can also use: This will raise a KeyError if the … Read more

How do I select rows from a DataFrame based on column values?

To select rows whose column value equals a scalar, some_value, use ==: To select rows whose column value is in an iterable, some_values, use isin: Combine multiple conditions with &: Note the parentheses. Due to Python’s operator precedence rules, & binds more tightly than <= and >=. Thus, the parentheses in the last example are necessary. Without the parentheses is parsed as which results in a Truth … Read more

How do I select rows from a DataFrame based on column values?

To select rows whose column value equals a scalar, some_value, use ==: To select rows whose column value is in an iterable, some_values, use isin: Combine multiple conditions with &: Note the parentheses. Due to Python’s operator precedence rules, & binds more tightly than <= and >=. Thus, the parentheses in the last example are necessary. Without the parentheses is parsed as which results in a Truth … Read more

Finding the average of a list

On Python 3.4+ you can use statistics.mean() On older versions of Python you can do On Python 2 you need to convert len to a float to get float division There is no need to use reduce. It is much slower and was removed in Python 3.

ln (Natural Log) in Python

math.log is the natural logarithm: From the documentation: math.log(x[, base]) With one argument, return the natural logarithm of x (to base e). Your equation is therefore: Note that in your code you convert n to a str twice which is unnecessary