美文网首页数据可视化
Pandas中的一些操作_01(2019-1-19)

Pandas中的一些操作_01(2019-1-19)

作者: MMatx | 来源:发表于2019-01-20 21:04 被阅读0次

    1、DataFrame中的selecting和reindex操作
    (1)选择selecting

    import pandas as pd
    imdb=pd.read_csv('movie_metadata.csv')
    imdb.head(4) #默认返回前5行
    imdb.shape
    imdb.tail()
    s1 =imdb['color']
    imdb[['color','director_name']].head() #如果选择多行则里面是一个列表
    sub_df = imdb[['color','director_name','movie_title']]
    sub_df.iloc[10:20,:] #返回10~20行
    sub_df.iloc[10:20,0:2]
    sub_df.loc[15:17,:'director_name'] #包括“director_name”
    

    (2)Reindex

    s1 = pd.Series([1, 2, 3, 4],index=['A','B','C','D'])
    s1.reindex(index=['A','B','C','D','E']) #原来不存在的索引,补充值为nan
    s1.reindex(index=['A','B','C','D','E'],fill_value=10)#把不存在的值填充为10
    s2= pd.Series(['A','B','C'],index=[1, 5, 10])
    s2.reindex(index=range(15))
    s2.reindex(index=range(15),method='ffill') # forward
    import numpy as np
    df1 = pd.DataFrame(np.random.rand(25).reshape(5,5),index=['A','B','D','E','F'],columns=['c1','c2','c3','c4','c5'])
    df1.reindex(index=['A','B','C','D','E','F'])
    df1.reindex(index=['A','B','C','D','E','F'])
    
    

    (3)Drop

    s1.drop('A') #去掉一行
    df1.drop('A',axis=0) #指定索引是行
    df1.drop('c1',axis=1)#指定删除的为列
    
    

    2、NAN值

    import string
    import pandas as pd
    import numpy as np
    n= np.nan
    type(n)
    m = 1
    m + n #与nan值相加,得到的结果为nan
    s1 = pd.Series([1, 2, np.nan,3 , 4],index=list(string.ascii_uppercase[:5]))
    s1.isnull() #返回是否为nan
    s1.notnull()
    s1.dropna() # 删除为nan的行
    # dataframe
    dframe = pd.DataFrame([[1,2,3],[np.nan,5,6],[7,np.nan,9],[np.nan,np.nan,np.nan]])
    dframe.isnull()
    dframe.notnull()
    dframe.dropna() #只要存在nan的行都被删掉
    dframe.dropna(axis=1) #存在nan的列都被删掉
    dframe.dropna(axis=0,how='all') # how值默认是any ,all是所有为nan才会删除
    dframe2= pd.DataFrame([[1,2,3,np.nan],[2,np.nan,5,6],[np.nan,np.nan,np.nan,9],[1,np.nan,np.nan,np.nan]])
    dframe2
    # thresh=2 是将大于2 的nan删掉
    dframe2.dropna(thresh=2)
    dframe2.fillna(value=1) #把为nan的位置都填充为2
    #列
    dframe2.fillna(value={0:1,1:5,2:7,3:8})
    
    

    3、多级index

    import numpy as np
    import pandas as pd
    s1 = pd.Series(np.random.rand(6),index=[['1','1','1','2','2','2'],['a','b','c','a','b','c']])
    In [5]:
    s1
    Out[5]:
    1  a    0.928077
       b    0.188681
       c    0.362011
    2  a    0.970636
       b    0.333167
       c    0.389710
    dtype: float64
    In [7]:
    s1['1']
    Out[7]:
    a    0.928077
    b    0.188681
    c    0.362011
    dtype: float64
    In [8]:
    s1['1']['a']
    Out[8]:
    0.9280770733545052
    In [10]:
    s1[:,'a']
    Out[10]:
    1    0.928077
    2    0.970636
    dtype: float64
    df1 = s1.unstack() #不堆叠,变成二维
    df1
    Out[17]:
    a   b   c
    1   0.928077    0.188681    0.362011
    2   0.970636    0.333167    0.389710
    In [15]:
    pd.DataFrame([s1['1'],s1['2']])
    Out[15]:
    a   b   c
    0   0.928077    0.188681    0.362011
    1   0.970636    0.333167    0.389710
    In [19]:
    df1.unstack()
    Out[19]:
    a  1    0.928077
       2    0.970636
    b  1    0.188681
       2    0.333167
    c  1    0.362011
       2    0.389710
    dtype: float64
    In [20]:
    s1
    Out[20]:
    1  a    0.928077
       b    0.188681
       c    0.362011
    2  a    0.970636
       b    0.333167
       c    0.389710
    dtype: float64
    In [22]:
    df1.T.unstack()
    Out[22]:
    1  a    0.928077
       b    0.188681
       c    0.362011
    2  a    0.970636
       b    0.333167
       c    0.389710
    dtype: float64
    In [27]:
    #dataframe
    df = pd.DataFrame(np.arange(16).reshape(4,4),index=[['a','a','b','b'],['1','1','2','2']],columns=[['BJ','BJ','SH','GZ'],[8,9,8,8]])
    df
    Out[27]:
    BJ  SH  GZ
    8   9   8   8
    a   1   0   1   2   3
    1   4   5   6   7
    b   2   8   9   10  11
    2   12  13  14  15
    In [28]:
    df['BJ']
    Out[28]:
    8   9
    a   1   0   1
    1   4   5
    b   2   8   9
    2   12  13
    In [29]:
    df['BJ'][8]
    Out[29]:
    a  1     0
       1     4
    b  2     8
       2    12
    Name: 8, dtype: int32
    

    相关文章

      网友评论

        本文标题:Pandas中的一些操作_01(2019-1-19)

        本文链接:https://www.haomeiwen.com/subject/vfsfjqtx.html