Pandas

pandas

1. print(pandas.__version__)

终端conda update pandas

2.

df = pd.read_csv("NBA Player.csv",sep = "\t")

3.

——

a = {"name":"xiaoming","age":20,"district":Beijin}

pd.Series(a)

——

b = [1,2,3,4,5]

pd.Series(b,index =list( "abcde"))

4. 

——

a = [[1,2,3],[7,8,9]]

pd.Dataframe(a,columns = list("ab"),index = list("ABC"))

5. 

df.columns  #列名

df.index  #索引

df.dtype  #返回每一列的数值类型(非数值都返回object)

df.shape

df.zise

len(df)

df.head()

df.tail()

df.rename = (columns = {"height":"Height"},inplace = True)  #inplace = True 修改原数据集

df.replace = ("Height":{A:B})

df.collage.value_counts()  #计数

df.sort_values(by = ["Height","Weight"]) 根据某些列值排序

 df.describe  #描述的是数据集里面所有的特征数

df.max(axis= 0)  #默认为axis = 0,针对列计算

6. #数据选取、添加、删除

——

df[["Height","Weight"]]

df.Height

——

df.["glass"] = 1  #每次只能增加一列

——

df[(df["Height"] > 200) | (df["Height"] < 50)]

——

del df["glass"]  #对本身做修改

7. #缺失值

pd.isnull(df)

pd.isnull(df["Height"])

df.dropna(axis =0,how = "any", thresh = None, inplace = False)

df.fillna(value = None,method = None,axis = None,inplace = False, limit = None, downcast = None,**kwargs)

8. #文本数据

——

s = pd.Series(["AB","abc","efg"])

s.str.strip().str.endwith("o")

——

df["Player"].str.split(" ").str.get(1)

df["Player"].split(" ",expand = True)

df["Player"].str[:3]

9.#索引操作

——

df[:5]  #返回5行

——

df.loc[:5]  #返回6行,loc基于lable,

df.loc[["Arai","Kalif"]]

df.loc[df[["Arai","Kalif"],["Height"]>= 180]]

——

df.iloc[:5]  #返回5行,基于位置

——

df.loc[(df['Height']>= 180) &(df['weight']>= 80),'flag'] = 'high'

df.loc[(df['Height']>= 180) &(df['weight']>= 170),'flag'] = 'msize'

df.loc[~(df['Height']>= 180) &(df['weight']>= 170),'flag'] = 'small'

10. #分组计算

grouped = df.groupby('director_name')

grouped.mean()

grouped['duration'].mean()

grouped.std()

import numpy as np

grouped.agg([np.mean,np.std])

grouped.agg({'duration':np.mean,'facebook_like':np.mean'})

11.#transformation 标准化

z_score = lambda s: (s-s.mean())/s.std()

grouped[['facebook_like','duration']].transformation(z_sccore)

12.#Filteration过滤

grouped.filter(lambda g: len(g)>1)['daration'].value_counts()

13.#表连接

result = concat([df1,df2,df3])

pd.merge(left,right,on = 'key')

你可能感兴趣的:(Pandas)