目录
一、提取数据
1. 从csv文件
pd.read_csv('/home/xxx/xxxx_0818.csv')
2. 从数据仓库
# 需要配置
spark.sql('''
select * from tbl
where member_id = '001'
''').toPandas()
二、新建
1. DataFrame
# 一共三列,列名:feature,ex,pvalue:
pd.DataFrame({'feature':[], 'ex':[], 'pvalue':[]}
# 带数据:
pd.DataFrame({'feature':index, 'ex':example, 'pvalue':p, 'chi':chi}, index = [1])
三、数据筛选
1. 判断空值
df_new[df_new['col1'].isnull() == False]
2. 根据一列数据筛选
dataframe[dataframe['col1']<0.05]
四、表之间
1. 横向合并(列相同情况下)
df_orig = df_orig.append(df_new, ignore_index=True)
2. 纵向合并
cols = gender.join(is_married)
cols = basic.join(age_segment1)
3. merge
member_level_merge = pd.merge(member_level, m_info, on = 'member_id', how = 'left')
五、表内
1. 排序
dataframe.sort_values(['col1'], ascending = False)
2. 去重
dataframe.drop_duplicates('member_id')
3. 分组计数
table['date_difference'].value_counts()
table.groupby('date_difference).size()
4. groupby( )
# 计数
df.groupby(['label1','label2']).size()
# 根据另外一列计算
df.groupby(['label1','label2'])['age'].mean()
df.groupby(['label1','label2'])['pay_amount'].sum()
5. iloc( )
table.iloc[:, 0:3] # 第一至三列
6. loc( )
wh.loc[i, 'col_name'] # 行数为i 列名为col_name
7. 循环每列
for index, row in dataframe.iteritems():
print(index) # 打印每列的列名
或者
for col in df.columns:
print(index) # 打印每列的列名
8. 删除/选取列
X = df.drop(['col1', 'col2', 'col3'], axis=1) # 删除列
y = df.label # 选取列
9. 按列名包含字符筛选
m_info[m_info['phone_brand'] in ['OPPO', 'VIVO', '三星']]
10. 去掉空值
all_Feat.dropna(how = 'any') # 去掉任意列是空值的行
all_Feat.dropna(how = 'all') # 去掉所有列是空值的行
11. 检查数据类型
df['member_id'].dtype
df['member_id'].dtype == 'object'
12. 检查空值
df['member_id'].isnull()
df['member_id'].notnull()
13. dataframe行列数
df.shape
df.shape[0] # 行数
六、Jupyter Notebook相关
1. 展示所有行/所有列
# 所有行
pd.set_option('display.max_rows', None)
# 所有列
pd.set_option('display.max_columns', None)
2. 自定义图片大小
# 定义ax
ax.figure.set_size_inches(8,36)
plt.show()