pandas


pandas series or tidy dataframe: index level values to dataframe columns


I have a tidy Dataframe (which I'm not sure how to produce from scratch) such as:
signal condition epoch time value
0 A 0 -1100 0.12
1 A 0 -1080 0.09
2 A 0 -1060 0.08
...
With several conditions A,B,C. I want to get a DataFrame with multiindex, and the values of value column as values in the new (and only) columns A, B, C so in the end it looks like:
value A B C
epoch time
0 -1100 0.12 0.23 0.09
-1080 0.09 0.22 0.10
-1060 0.08 0.19 0.06
...
I tried to first put everything except value into the index with set_index(['condition','epoch','time']) and then transpose or pivot but can't get it right somehow (transpose gives me hierarchical columns and pivot KeyError)
I tried e.g.: df.pivot(('epoch','time'),'condition')
You were on the right track using pivot which is explicitly designed for such long-to-wide transformations:
# show dummy df
print(df)
signal condition epoch time value
0 0 A 0 -1100 0.12
1 1 A 0 -1080 0.09
2 2 A 0 -1060 0.08
3 0 B 0 -1100 0.42
4 1 B 0 -1080 0.29
5 2 B 0 -1060 0.18
6 0 C 0 -1100 0.32
7 1 C 0 -1080 0.59
8 2 C 0 -1060 0.38
# pivot
pd.pivot_table(df, values="value", columns="condition", index=["epoch", "time"])
condition A B C
epoch time
0 -1100 0.12 0.42 0.32
-1080 0.09 0.29 0.59
-1060 0.08 0.18 0.38
try
df.set_index(['epoch', 'time', 'condition']).value.unstack().rename_axis('value', 1)
value A B C
epoch time
0 -1100 0.12 0.23 0.09
-1080 0.09 0.22 0.10
-1060 0.08 0.19 0.06
setup
import pandas as pd
from io import StringIO
txt = """signal condition epoch time value
0 A 0 -1100 0.12
1 A 0 -1080 0.09
2 A 0 -1060 0.08
0 B 0 -1100 0.23
1 B 0 -1080 0.22
2 B 0 -1060 0.19
0 C 0 -1100 0.09
1 C 0 -1080 0.10
2 C 0 -1060 0.06 """
df = pd.read_csv(StringIO(txt), delim_whitespace=True)

Related Links

How do I find the closest values in a Pandas series to an input number?
Calculating group average and assigning it to sub-group using Pandas groupby
Pandas bar plot changes date format
pandas pie chart plot remove the label text on the wedge
pandas: optimizing my code (groupby() / apply())
Indexing on Pandas Grouby Data frame Gives error
Python 3.4 Pandas DataFrame Structuring
Python3.4 Pandas DataFrame from function
Select the row and column element of a dataframe and decide the regression variables
Apply function with pandas dataframe - POS tagger computation time
pandas: trouble transforming dataframe into aggregated dataframe
Python: Pandas to latex - Issues with the backslash
dropping rows in pandas dataframe based on column entries
Pandas dataframe apply function
Python/Pandas: counting the number of missing/NaN in each row
Unable to call value_counts on a new column

Categories

HOME
yii2
cookies
hashmap
kalman-filter
spring-cloud-stream
react-redux
fsm
angular-ui-bootstrap
blueprintjs
mvvmcross
serverspec
django-imagekit
amazon-cloudformation
facebook-php-sdk
vault
tomcat6
node-pdfkit
user-input
volttron
izpack
remote-access
event-handling
windows-server-2012
decimal
dynamics-crm-online
angular-ui
pythonanywhere
swingx
cloudhub
format-specifiers
qhull
filezilla
xacml
msys2
kendo-datasource
sql-server-2012-express
bosh
twilio-api
.net-4.0
fog
commit
lxd
language-concepts
google-api-nodejs-client
bitbucket-pipelines
retina-display
automake
multilingual
code-search-engine
fedex
gzipstream
quadratic-programming
android-mediaprojection
eigenvalue
modelmapper
xml-documentation
pearson
memory-alignment
color-profile
dynamics-sl
vmware-tools
nsfilemanager
storekit
websitepanel
kendonumerictextbox
wso2cloud
lib.web.mvc
comobject
wyam
angular-leaflet-directive
remobjects
p2
dia
mesa
hyprlinkr
algebraic-data-types
dotnetnuke-5
seed
eventlistener
selected
quickdialog
manchester-syntax
appconkit
enter
fireworks
gwt-ext
vdsp
javax.script
self-extracting
ecl
ext3
virtual-functions
spec#
mediarss
scripting-languages
defensive-programming
dbisam

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