1. 读excel 1000行:
pd.read_excel('path', nRows=1000)
head(10), tail(10)
2. 选择某几行:
iloc[1], iloc[1:9], iloc[:10],loc['indexA'], loc['indexA':'indexC']
3. 选择某几列:
df['colA'],df.colA,df[['cloA', 'colB', 'colC']]
4. 选择符合条件的某几行:
df[df['colA'] > 10]
df[(df['colA'] > 10) & (df['colB'] == 'test')]
df.query('(colA > 10) & (colB == "test")')
df.where('(colA > 10) & (colB == "test")')
isin ---> df[df['colA'].isin({['A', 'B']})]
5. 选择符合条件的某几行的某几列:
df.loc('(colA > 10) & (colB == "test")', ['colC', 'colD', 'colE'])
6. 删除列:
df.drop(columns = ['colA', 'colB'])
7. 增加列:
df['new_col'] = {'key' : [1,2,3,4,5]}
df['new_col'] = df['colA'] * df['colB']
df['new_col'] = df['colA'].apply(lambda x : x **2)
df['new_col'] = df['colA'].apply(lambda x : str(x) + '_' + x)
8. 看某一列不一样的值都有哪些:
df['actual_weight'].unique()
9. 看某一列不一样的值有几个:
df['actual_weight'].nunique()
10. 看某列,每个元素有多少个,相当于groupBy:
df['actual_weight'].value_counts()
idxmax:某列最大值所在的索引位置 df['colA'].idxmax()
idxmin:某列最小值所在的索引位置 df['colA'].idxmin()
11. 按照某一列排序
df.sort_values(by = ['colA', 'colB', 'colC'])
12. 某一列 非缺失值的个数:
df['colA'].count()
13. cut的用法:
c = pd.DataFrame({'math' : [21, 39, 20, 11, 98, 72]})
bins = [0, 20,40,80,90,100]
c['cuts'] = pd.cut(c['math'], bins)
c.groupby( by = ['cuts']).count()
14. 两个DF结合:
df1[['id', 'poi_id']].join(df2'process_date'])
15. 两个DF合并:
df1.append(df2)