Pandas练习笔记——(二)

import numpy as np

import pandas as pd

data = {'color':['blue','green','yellow','red','white'],'object':['ball','pen','pencil','paper','mug'],'price':[1.2,1.0,2,2.1,0.5]}

frame = pd.DataFrame(data)

frame
Out[5]: 
    color  object  price
0    blue    ball    1.2
1   green     pen    1.0
2  yellow  pencil    2.0
3     red   paper    2.1
4   white     mug    0.5

frame2 = pd.DataFrame(data,columns=['object','price'])

frame2
Out[7]: 
   object  price
0    ball    1.2
1     pen    1.0
2  pencil    2.0
3   paper    2.1
4     mug    0.5

frame2 = pd.DataFrame(data,index=['one','two','three','four','five'])

frame2
Out[9]: 
        color  object  price
one      blue    ball    1.2
two     green     pen    1.0
three  yellow  pencil    2.0
four      red   paper    2.1
five    white     mug    0.5

frame3 = pd.DataFrame(np.arange(16).reshape((4,4)),index=['red','blue','yellow','white'],columns=['ball','pen','pencil','paper'])

frame3
Out[11]: 
        ball  pen  pencil  paper
red        0    1       2      3
blue       4    5       6      7
yellow     8    9      10     11
white     12   13      14     15

frame3.columns
Out[12]: Index(['ball', 'pen', 'pencil', 'paper'], dtype='object')

frame3.values
Out[13]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

frame
Out[15]: 
    color  object  price
0    blue    ball    1.2
1   green     pen    1.0
2  yellow  pencil    2.0
3     red   paper    2.1
4   white     mug    0.5

frame
Out[17]: 
    color  object  price
0    blue    ball    1.2
1   green     pen    1.0
2  yellow  pencil    2.0
3     red   paper    2.1
4   white     mug    0.5

frame['price']
Out[18]: 
0    1.2
1    1.0
2    2.0
3    2.1
4    0.5
Name: price, dtype: float64

frame.price
Out[19]: 
0    1.2
1    1.0
2    2.0
3    2.1
4    0.5
Name: price, dtype: float64

# 获取DataFrame中的行

frame.ix[2]
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.
Out[21]: 
color     yellow
object    pencil
price          2
Name: 2, dtype: object

frame.ix[[2,4]]
Out[22]: 
    color  object  price
2  yellow  pencil    2.0
4   white     mug    0.5

frame[1:3]
Out[23]: 
    color  object  price
1   green     pen    1.0
2  yellow  pencil    2.0

frame['object'][3]
Out[24]: 'paper'

frame.index.name = 'id'

frame
Out[28]: 
item   color  object  price                   
0      blue    ball    1.2
1      green     pen    1.0
2      yellow  pencil    2.0
3      red   paper    2.1
4      white     mug    0.5

frame['news'] = 12

frame
Out[30]: 
item   color  object  price  news                            
0      blue    ball    1.2    12
1      green     pen    1.0    12
2      yellow  pencil    2.0    12
3      red   paper    2.1    12
4      white     mug    0.5    12

frame
Out[33]: 
item   color  object  price  news                            
0      blue    ball    1.2    12
1      green     pen    1.0    12
2      yellow  pencil    2.0    12
3      red   paper    2.1    12
4      white     mug    0.5    12

frame['new'] = [1,2,3,4,5]

frame
Out[35]: 
item   color  object  price  news  new                                  
0      blue    ball    1.2    12    1
1      green     pen    1.0    12    2
2      yellow  pencil    2.0    12    3
3      red   paper    2.1    12    4
4      white     mug    0.5    12    5

ser = pd.Series(np.arange(5))

ser
Out[37]: 
0    0
1    1
2    2
3    3
4    4
dtype: int32

frame['new2'] = ser

frame
Out[39]: 
item   color  object  price  news  new  new2                                       
0      blue    ball    1.2    12    1     0
1      green     pen    1.0    12    2     1
2      yellow  pencil    2.0    12    3     2
3      red   paper    2.1    12    4     3
4      white     mug    0.5    12    5     4

frame['price'][2] = 3.3
D:\ProgramData\Anaconda3\lib\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.

frame
Out[41]: 
item     color  object  price  news  new  new2                                     
0        blue    ball    1.2    12    1     0
1        green     pen    1.0    12    2     1
2        yellow  pencil    3.3    12    3     2
3        red   paper    2.1    12    4     3
4        white     mug    0.5    12    5     4

frame2
Out[42]: 
        color  object  price
one      blue    ball    1.2
two     green     pen    1.0
three  yellow  pencil    2.0
four      red   paper    2.1
five    white     mug    0.5

frame.isin([2.0,'pen'])
Out[43]: 
item    color  object  price   news    new   new2                                         
0       False   False  False  False  False  False
1       False    True  False  False   True  False
2       False   False  False  False  False   True
3       False   False  False  False  False  False
4       False   False  False  False  False  False

frame2.isin([2.0,'pen'])
Out[44]: 
       color  object  price
one    False   False  False
two    False    True  False
three  False   False   True
four   False   False  False
five   False   False  False

frame2[frame2.isin([2.0,'pen'])]
Out[45]: 
      color object  price
one     NaN    NaN    NaN
two     NaN    pen    NaN
three   NaN    NaN    2.0
four    NaN    NaN    NaN
five    NaN    NaN    NaN

del frame['new2']

frame
Out[47]: 
item    color  object  price  news  new                                  
0       blue    ball    1.2    12    1
1       green     pen    1.0    12    2
2       yellow  pencil    3.3    12    3
3       red   paper    2.1    12    4
4       white     mug    0.5    12    5

frame3
Out[48]: 
        ball  pen  pencil  paper
red        0    1       2      3
blue       4    5       6      7
yellow     8    9      10     11
white     12   13      14     15

frame3[frame3<12]
Out[49]: 
        ball  pen  pencil  paper
red      0.0  1.0     2.0    3.0
blue     4.0  5.0     6.0    7.0
yellow   8.0  9.0    10.0   11.0
white    NaN  NaN     NaN    NaN

nestdict = {'red':{2012:22,2013:33},'white':{2011:13,2012:22,2013:16},'blue':{2011:17,2012:27,2013:18}}

frame = pd.DataFrame(nestdict)

frame
Out[52]: 
      blue   red  white
2011    17   NaN     13
2012    27  22.0     22
2013    18  33.0     16

frame.T
Out[53]: 
       2011  2012  2013
blue   17.0  27.0  18.0
red     NaN  22.0  33.0
white  13.0  22.0  16.0

 

posted @ 2018-01-10 13:00  风在人舟  阅读(815)  评论(0编辑  收藏  举报