python


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[761]*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.

Related Links

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

Categories

HOME
spring
cntk
ibm-watson-cognitive
npm
winapi
angular-cli
textwatcher
translation
formal-verification
amazon-product-api
command
appmaker
hyperledger-fabric
filter
internet-explorer-11
actionscript-2
uiscrollview
apache-karaf
grub2
primes
kadanes-algorithm
nmake
nsmenuitem
rworldmap
extractor
http-method
novnc
scalajs-react
opencart2.3
mef2
pentaho-report-designer
winscp
pcre
superagent
twitter-bootstrap-2
picturebox
exponential
graph-databases
.net-assembly
discord.net
wcf-security
tripwire
jrules
resuming-training
rdw
rhel6
component-pascal
forerunnerdb
volume
jexl
date-range
build-process
windows-azure-pack
django-filer
inotifypropertychanged
multilingual-app-toolkit
squirrel
fps
picking
concur
amazon-kcl
servlet-3.0
void
throughput
java.util.calendar
hornetq
affix
mirrorlink
wif
calibration
pgm
git-checkout
jai
cloudpebble
cg
google-admin-audit-api
ogr2ogr
dalekjs
visualstatemanager
visual-studio-addins
windows-mobile-6
fogbugz-api
facebook-sdk-3.1
sttwitter
shiva3d
mailcore
magickwand
cbcentralmanager
returnurl
datarepeater
deploying
selectonemenu
wchar
electronic-signature
scraperwiki
coff
audiostreamer
clause
hardware-acceleration
graniteds
asp.net-mvc-controller
kpi

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App