Multiplying a dataframe by a column… but values are strings?
I have a Dataframe that includes binary variables about respondents' behavior and the weight associated with each respondent. I'd like to multiply the scores by each respondents' weight so I can easily get a weighted average for the total behavior. The easiest thing would be to multiply the weight column against another column in a loop, as in df.columns*df.columns[i]. However, when I try to do, it throws an error of: 'can't multiply sequence by non-int of type 'str'.' I shouldn't have any strings, but in the off-chance there are, I tried to convert the df to numeric, like so df.apply(pd.to_numeric, errors='coerce'). But the problem still remains. I'm at my wits' end. Is there a workaround? Should I go row by row (and if so, do I need to loop through every column, or is there a nice clean way?).
You could always break apart your dataframe. for col in df.columns: for index, k in enumerate(df[col]): try: float(k) except: # Print out the row number, col and row value that's failing print(index, col, k) It's entirely possible you've got strings/none-types that are causing your multiplication. There's also df[col].apply(float) but it won't catch those errant rows.
Installing libqrencode on windows for python
Keeping the old value when a division by zero occurs
Error in running Scapy Sniff function
Converting a list to a dictionary troubles
pandas groupby numeric functions coerce numeric to object when DataFrame contains object
Python Multiprocessing does not wait
SqlAlchemy and PostgreSql datetime update
How do I use SQLalchemy to treat an existing select object as a view?
Deducting the median from each column
How to correctly instance a IPython cluster made of local and remote machines
Pandas: Efficient way to get first row with element that is smaller than a given value
Repeat function python
Code for -Python plot
How can I use a variable value to call a dataframe?
how to post multiple value with same key in python requests?
Encryption decryption of natural language model