Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas pivot

>>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
...                            'two'],
...                    'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
...                    'baz': [1, 2, 3, 4, 5, 6],
...                    'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
>>> df

>>df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])
bar  A   B   C
foo
one  1   2   3
two  4   5   6
Comment

Pandas pivot table

table = pd.pivot_table(df, values='D', index=['A', 'B'],
...                     columns=['C'], aggfunc=np.sum)
>>> table
C        large  small
A   B
bar one    4.0    5.0
    two    7.0    6.0
foo one    4.0    1.0
    two    NaN    6.0
Comment

Pandas pivot table

>>> emp.pivot_table(index='dept', columns='gender',                     values='salary', aggfunc='mean').round(-3)
Comment

pivot table pandas

# Simplest pivot table must have a dataframe
# and an index/list of index.
table = pd.pivot_table(df, index =['A', 'B'])
  
table
Comment

pandas pivot tables

>>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
...                            'two'],
...                    'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
...                    'baz': [1, 2, 3, 4, 5, 6],
...                    'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
>>> df
    foo   bar  baz  zoo
0   one   A    1    x
1   one   B    2    y
2   one   C    3    z
3   two   A    4    q
4   two   B    5    w
5   two   C    6    t

>>> df.pivot(index='foo', columns='bar', values='baz')
bar  A   B   C
foo
one  1   2   3
two  4   5   6
Comment

pd df pivot

df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])
Comment

pd df pivot table

table = pd.pivot_table(df, values='D', index=['A', 'B'],
...                     columns=['C'], aggfunc=np.sum, fill_value=0)
Comment

pd df pivot table

table = pd.pivot_table(df, values='D', index=['A', 'B'],
...                     columns=['C'], aggfunc=np.sum)
Comment

pandas from pivot to dataframe

df = pivot_name.reset_index()
Comment

pivot table pandas

df.pivot_table(index = [df.iloc[:,meet_friends], df.iloc[:,friendsgiving]])
Comment

pivot table pandas

df.pivot_table(['int_age'],index = [df.iloc[:,meet_friends], df.iloc[:,friendsgiving]])
Comment

pd df pivot

df.pivot(index='foo', columns='bar', values='baz')
Comment

pd df pivot

df.pivot(index="lev1", columns=["lev2", "lev3"],values="values")
Comment

PREVIOUS NEXT
Code Example
Python :: replace multiple values in pandas column 
Python :: python for k, v in dictionary 
Python :: python add to file 
Python :: python kill all threads 
Python :: return count of substring in a string 
Python :: How to install pandas-profiling 
Python :: find character in python 
Python :: esp8266 micropython ds18b20 
Python :: hasattr in python 
Python :: python numpy array change axis 
Python :: flask quickstart 
Python :: making lists with loops in one line python 
Python :: ImportError: /home/user/.local/lib/python3.8/site-packages/pytorch3d/_C.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN2at5zerosEN3c108ArrayRefIlEENS0_13TensorOptionsE 
Python :: how to create model in tensorflow 
Python :: Select rows in pandas MultiIndex DataFrame 
Python :: how to print answer 2 decimal places in python 3 
Python :: the boys 
Python :: print variable name 
Python :: simple way of finding file extension python programming 
Python :: System.Windows.Forms.DataGridView.CurrentRow.get returned null. c# 
Python :: how to remove tkinter icon 
Python :: wait in python 
Python :: 2d gaussian function python 
Python :: list all files starting with python 
Python :: print elements without print function in python 
Python :: python how to draw a square 
Python :: python sort columns of pandas dataframe 
Python :: python timedelta to seconds 
Python :: how to find a word in list python 
Python :: drop na dataframe 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =