python数据框dataframe功能总结(转载)

1.数据框增加一列,df['new'] = test[0]表示给df新增一列,列名为new,整列值为test[0];

  1.  
    import pandas as pd
  2.  
     
  3.  
    a = {'ip':[1,2,3,4],'name':[0,9,8,77],'time':['qq','ww','ee','rr']}
  4.  
    df = pd.DataFrame(a)
  5.  
    test = ['qqq','eeee']
  6.  
    df['new'] = test[0]
  7.  
    print(df)

2.数据框列内容筛选,

df1 = df[df['name'].isin([9,8])]表示筛选name列为9和8的数据框;

df2 = df[~df['name'].isin([9,8])]表示筛选name列不是9和8的数据框,~表示反;

df3 = df[(df['name'] > 9) | (df['name'] < 8)]表示筛选name列大于9或小于8的数据框,|表示或;
df4 = df[(df['ip'] > 2) & (df['ip'] <= 4)]表示筛选ip列大于2且小于等于4的数据框,&表示且。

  1.  
    import pandas as pd
  2.  
     
  3.  
    a = {'ip':[1,2,3,4],'name':[0,9,8,77],'time':['qq','ww','ee','rr']}
  4.  
    df = pd.DataFrame(a)
  5.  
    df1 = df[df['name'].isin([9,8])]
  6.  
    df2 = df[~df['name'].isin([9,8])]
  7.  
    df3 = df[(df['name'] > 9) | (df['name'] < 8)]
  8.  
    df4 = df[(df['ip'] > 2) & (df['ip'] <= 4)]
  9.  
    print(df1)
  10.  
    print(df2)
  11.  
    print(df3)
  12.  
    print(df4)

3.数据框,获取行数、列数及第几行第几列的值

  1.  
    import pandas as pd
  2.  
     
  3.  
    a = {'ip':[1,2,3,4],'name':[0,9,8,77],'time':['qq','ww','ee','rr']}
  4.  
    df = pd.DataFrame(a)
  5.  
    print(df)
  6.  
    colum_size = df.columns.size#列数 2
  7.  
    row_size = df.iloc[:,0].size#行数 3
  8.  
    num1 = df.iloc[[0]].values[0][0]#第一行第一列的值 1
  9.  
    num2 = df.iloc[[1]].values[0][1]#第二行第二列的值 9
  10.  
    print(colum_size)
  11.  
    print(row_size)
  12.  
    print(num1)
  13.  
    print(num2)

4.数据框固定列顺序

  1.  
    import pandas as pd
  2.  
     
  3.  
    a = {'ip':[1,2,3,4],'name':[0,8,8,77],'time':['qq','ww','ee','rr']}
  4.  
    df = pd.DataFrame(a)
  5.  
    b = ['name','ip','time']
  6.  
    df1 = df.loc[:,b]
  7.  
    print(df1)

 

posted @ 2020-08-31 14:17  Kot  阅读(2502)  评论(0编辑  收藏  举报