item color object price id 0 blue ball 1.2 1 green pen 1.0 2 yellow pencil 0.6 3 red paper 0.9 4 white mug 1.7
1 2
frame[‘new‘] = 12 frame
item color object price new id 0 blue ball 1.2 12 1 green pen 1.0 12 2 yellow pencil 0.6 12 3 red paper 0.9 12 4 white mug 1.7 12
1 2
frame[‘new‘] = [3.0,1.3,2.2,0.8,1.1] frame
item color object price new id 0 blue ball 1.2 3.0 1 green pen 1.0 1.3 2 yellow pencil 0.6 2.2 3 red paper 0.9 0.8 4 white mug 1.7 1.1
1 2
ser = pd.Series(np.arange(5)) ser
0 0
1 1
2 2
3 3
4 4
dtype: int64
1 2
frame[‘new‘] = ser frame
item color object price new id 0 blue ball 1.2 0 1 green pen 1.0 1 2 yellow pencil 0.6 2 3 red paper 0.9 3 4 white mug 1.7 4
1 2
frame[‘price‘][2] = 3.3 frame
/home/vivoadmin/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
"""Entry point for launching an IPython kernel.
item color object price new id 0 blue ball 1.2 0 1 green pen 1.0 1 2 yellow pencil 3.3 2 3 red paper 0.9 3 4 white mug 1.7 4
red 0 1 2 3 blue 4 5 6 7 yellow 8 9 10 11 white 12 13 14 15
1
frame.sort_index()
ball pen pencil paper
blue 4 5 6 7 red 0 1 2 3 white 12 13 14 15 yellow 8 9 10 11
1
frame.sort_index(axis=1)
ball paper pen pencil
red 0 3 1 2 blue 4 7 5 6 yellow 8 11 9 10 white 12 15 13 14
1
ser.order()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-131-bb828ac4164f> in <module>()
----> 1 ser.order()
~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
3612 if name in self._info_axis:
3613 return self[name]
-> 3614 return object.__getattribute__(self, name)
3615
3616 def __setattr__(self, name, value):
AttributeError: ‘Series‘ object has no attribute ‘order‘
1
frame.sort_index(by=‘pen‘)
/home/vivoadmin/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: by argument to sort_index is deprecated, please use .sort_values(by=...)
"""Entry point for launching an IPython kernel.
ball pen pencil paper
red 0 1 2 3 blue 4 5 6 7 yellow 8 9 10 11 white 12 13 14 15
1
frame.sort_index(by=[‘pen‘,‘pencil‘])
/home/vivoadmin/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: by argument to sort_index is deprecated, please use .sort_values(by=...)
"""Entry point for launching an IPython kernel.
ball pen pencil paper red 0 1 2 3 blue 4 5 6 7 yellow 8 9 10 11 white 12 13 14 15
1
ser.rank()
red 4.0
blue 1.0
yellow 2.0
white 5.0
green 3.0
dtype: float64
1
ser.rank(method=‘first‘)
red 4.0
blue 1.0
yellow 2.0
white 5.0
green 3.0
dtype: float64
1
ser.rank(ascending=False)
red 2.0
blue 5.0
yellow 4.0
white 1.0
green 3.0
dtype: float64
1 2 1 2 white up 0.992202 -1.037634 -0.083882 -0.028204 down -1.758579 1.832742 -0.167739 0.174567 red up 0.983876 2.306897 -1.003258 -0.591767 down 1.398166 0.054964 -1.705692 -0.375007
id 1 2 1 2 colors status white up 0.992202 -1.037634 -0.083882 -0.028204 down -1.758579 1.832742 -0.167739 0.174567 red up 0.983876 2.306897 -1.003258 -0.591767 down 1.398166 0.054964 -1.705692 -0.375007
1
mframe.swaplevel(‘colors‘,‘status‘)
objects pen paper
id 1 2 1 2 status colors up white 0.992202 -1.037634 -0.083882 -0.028204 down white -1.758579 1.832742 -0.167739 0.174567 up red 0.983876 2.306897 -1.003258 -0.591767 down red 1.398166 0.054964 -1.705692 -0.375007
1
mframe.sortlevel(‘colors‘)
/home/vivoadmin/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: sortlevel is deprecated, use sort_index(level= ...)
"""Entry point for launching an IPython kernel.
objects pen paper
id 1 2 1 2 colors status red down 1.398166 0.054964 -1.705692 -0.375007 up 0.983876 2.306897 -1.003258 -0.591767 white down -1.758579 1.832742 -0.167739 0.174567 up 0.992202 -1.037634 -0.083882 -0.028204
4.12.2 按层级统计数据 89
1
mframe.sum(level=‘colors‘)
objects pen paper id 1 2 1 2 colors white -0.766377 0.795107 -0.251622 0.146363 red 2.382042 2.361861 -2.708950 -0.966774
1
mframe.sum(level=‘id‘, axis=1)
id 1 2
colors status white up 0.908320 -1.065838 down -1.926319 2.007309 red up -0.019382 1.715131 down -0.307526 -0.320044