http://www.reibang.com/p/334bb48f6e43
--數(shù)據(jù)洞察
df.shape? #查看數(shù)據(jù)維度
df.info()? #查看數(shù)據(jù)信息
df.dtypes? df['B'].dtype? df.B.dtype? #查看列的數(shù)據(jù)類型
df.isnull()? ? df['B'].isnull()? df.B.isnull()? #空值檢查
df['B'].unique()? #查看列中的唯一值
df.values? #查看數(shù)據(jù)值
df.columns? #查看各列的名稱
df.head()? ? df.head(10)? #查看前N行數(shù)據(jù)
df.tail()? df.tail(10)? #查看后N行數(shù)據(jù)
--數(shù)據(jù)清洗
df.dropna(how = 'any')? #刪除含有空值的行
df.fillna(value = 0)? #用0來填充空值
df['B'].fillna(df['B'].mean())? #使用列均值來填充
df['B']=df['B'].map(str.strip)? #刪除前后空格
df['B']=df['B'].str.lower()? df['B']=df['B'].str.upper()? #大小寫轉(zhuǎn)化
df['B'].astype('int')? #數(shù)據(jù)類型轉(zhuǎn)換
df.rename(columns={'B': 'B1'})? #列的重命名
df['B'].drop_duplicates()? df['B'].drop_duplicates(keep='last')? #去除重復(fù)項(xiàng)
df['B'].replace('value1', 'value2')? #數(shù)據(jù)值的替換
--數(shù)據(jù)預(yù)處理
df = pd.merge(df1,df2,how='inner')? #類似excel中的vlookup,SQL中的join how(inner,left,right,outer)
df.set_index('id')? #設(shè)置索引列
df.sort_index()? #按索引列排序
df.sort_values(by=['B'])? #按特定的列的值排序
df['G'] = np.where(df['B'] > 10,'type_a','type_b')? #計(jì)算列与柑,如果B列的值>10圾旨,G列顯示type_a对省,否則顯示type_b
df.loc[(df['B'] == 'xxx') & (df['C'] >= 10), 'flag']='tag'? #計(jì)算列绊谭,多條件判斷派生新列
df1=pd.DataFrame((x.split('-') for x in df['B']),index=df.index,columns=['B1','B2'])
df=pd.merge(df,df1,right_index=True, left_index=True)? #按特定字符對(duì)列數(shù)據(jù)進(jìn)行分割丹泉,分割后與原數(shù)據(jù)合并
--數(shù)據(jù)獲取
#loc 按標(biāo)簽值獲取
#iloc 按位置獲取
#ix 可同時(shí)按標(biāo)簽值和位置進(jìn)行獲取
df.loc[2]? df.loc[0:5]
df.iloc[1:2,3:4]? df.iloc[:3,:2]
df.iloc[[1,3,5],[2,4,6]]
df.loc[df['B'].isin(['x','xx'])]? #按條件獲取數(shù)據(jù)
--數(shù)據(jù)篩選
df.loc[(df['B'] > 10) & (df['C'] == 'xx'), ['A','B','C','D','E']]
df.loc[(df['B'] > 10) | (df['C'] == 'xx'), ['A','B','C','D','E']]
df.loc[(df['B'] > 10) & (df['C'] == 'xx'), ['A','B','C','D','E']].sort(['A','E'])
df.loc[(df['B'] > 10) & (df['C'] == 'xx'), ['A','B','C','D','E']].D.sum()
df.query('B== ["xx", "xxx"]')
df.query('B== ["xx", "xxx"]').D.sum()