Format string unused named arguments

Let’s say I have: action = '{bond}, {james} {bond}'.format(bond='bond', james='james') this wil output: 'bond, james bond' Next we have: action = '{bond}, {james} {bond}'.format(bond='bond') this will output: KeyError: 'james' Is there some workaround to prevent this error to happen, something like: if keyrror: ignore, leave it alone (but do parse others) compare format string with … Read more

Pandas Dataframe: Replacing NaN with row average

I am trying to learn pandas but I have been puzzled with the following. I want to replace NaNs in a DataFrame with the row average. Hence something like df.fillna(df.mean(axis=1)) should work but for some reason it fails for me. Am I missing anything, is there something wrong with what I’m doing? Is it because its not implemented? see link here

How to get Python to gracefully format None and non-existing fields

If I write in Python: data = {'n': 3, 'k': 3.141594, 'p': {'a': 7, 'b': 8}} print('{n}, {k:.2f}, {p[a]}, {p[b]}'.format(**data)) del data['k'] data['p']['b'] = None print('{n}, {k:.2f}, {p[a]}, {p[b]}'.format(**data)) I get: 3, 3.14, 7, 8 Traceback (most recent call last): File "./funky.py", line 186, in <module> print('{n}, {k:.2f}, {p[a]}, {p[b]}'.format(**data)) KeyError: 'k' Instead of an … Read more