python


Pandas: count variance in pivot_table


I use
df = df.pivot_table(index='ID', columns='domain', values='active_seconds', aggfunc=np.var)
to return variance
But I need to add argument ddof=1 to function, because result is wrong.
How can I do that?
Use lambda:
aggfunc=lambda x: np.var(x, ddof=1)
Or use GroupBy.var with default ddof=1:
aggfunc='var'
Sample:
np.random.seed(10)
df = pd.DataFrame({'ID':np.random.choice(list('def'),size=30),
'domain':np.random.choice(list('abc'),size=30),
'active_seconds':np.random.randint(10,size=30)})
print (df.head())
ID active_seconds domain
0 e 1 c
1 e 7 b
2 d 1 b
3 d 4 b
4 e 0 b
df1 = df.pivot_table(index='ID',
columns='domain',
values='active_seconds',
aggfunc=lambda x: np.var(x, ddof=1))
print (df1)
domain a b c
ID
d 8.333333 8.333333 10.000000
e 8.000000 11.300000 16.333333
f 19.000000 8.000000 24.500000
df1 = df.pivot_table(index='ID', columns='domain', values='active_seconds', aggfunc='var')
print (df1)
domain a b c
ID
d 8.333333 8.333333 10.000000
e 8.000000 11.300000 16.333333
f 19.000000 8.000000 24.500000

Related Links

Creating audio file with gTTS for Google Speech API [duplicate]
Python: Make raw input remember commands [duplicate]
Can Omnicomplete in Vim for python/3 complete function names or just methods?
Slowness on iterating over namedtuple and dicts for defined pairs
I'm trying to put these inputs into a text file, [duplicate]
Printing Dictionaries? [closed]
OpenCV's frame count always printing 0
Including first and last elements in list comprehension
how to see output history on pycharm?
plotting just a single rgb color in matplotlib
Trying to edit an entry, but DB does not update
pymc unexpected model output
Drop columns by name that appear in multiple pandas dataframes
Anaconda makes conflicts with shared library's location? OSError: cannot open shared object file
PyQt and QML: How can multiple signals be processed in one slot or function
Selenium Webdriver (Firefox) timeout not working properly

Categories

HOME
caching
mql4
elixir
dynamics-crm
bower
smartphone
thunderbird-addon
extract
adsense
iis-7.5
schemacrawler
browserify
vsftpd
powershell-v3.0
ndis
nixos
android-source
google-shopping
abcpdf
sign
event-log
cloudflare
uisplitviewcontroller
ssms-2016
reduction
primes
backpropagation
autolayout
rworldmap
excel-2013
thinking-sphinx
forum
xor
svnkit
m2e
freebase
shapes
npm-shrinkwrap
pox
mmdrawercontroller
zero
xamarin.uitest
axis-labels
image-compression
haskell-pipes
web-development-server
google-account
dojox.mobile
fqdn
walmart-electrode
jags
web-deployment-project
search-box
uicollectionviewlayout
pdfkit
verbose
ebtables
easing
rstudio-server
aescryptoserviceprovider
git-annex
spring-lemon
achievements
fps
faraday
django-south
inet
interactive-brokers
eml
risk-analysis
vine
team-build
xjc
rspec3
proximity
google-admin-audit-api
planetary.js
android-looper
jstack
sim900
manage.py
snapjs
osi
usn
symfony-2.0
hibernate3
file-exists
text-services-framework
flexicious
msgbox
paintcomponent
gtk2hs
msbuildextensionpack
ets
nvelocity
requestfactory
winverifytrust
graniteds
rtsp-client
iphone-sdk-3.2

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