朝法夕拾

方法笔记

pandas

1,替换

df.replace({-999: pd.np.NaN})  
#用Nan替换-999,字典可以改变多个参数  
#参数inplace = True 改变源文件数据
#np.NaN 空值

2,填充缺失数据

DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
#method :backfill、bfill(向前填充)、pad、ffill(向后填充)

3,重采样

df = df.resample('D').mean()
#同一天数据平均值重采样
df.reset_index()   #重采样后要reset index

4,列名重命名

df.rename(columns={'Date (LST)': 'ds', 'Value': 'y'}, inplace=True)
#mapper, index, columns : 映射的规则。
#axis:指定轴,可以是轴名称('index','columns')或数字(0,1),默认为index。
#inplace:布尔值,默认为False。指定是否返回新的DataFrame。如果为True,则在原df上修改,返回值为

5,时间索引转换

df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)

6,删除行或列

df.drop('apps') # 删除行:
df.dorp('col', axis=1)#删除列:删除列要加axis=1,默认是删除行的
df.dropna(axis=1)#删除缺失行
data.dropna(how = 'all')    # 传入这个参数后将只丢弃全为缺失值的那些行,how = 'any':只要有缺失值就删除
data.dropna(axis=0,subset = ["Age", "Sex"])   # 丢弃‘Age’和‘Sex’这两列中有缺失值的行
data.dropna()#详细:https://www.cnblogs.com/sunbigdata/p/7895738.html
data.drop_duplicates(subset='LieMing', inplace=True)  # 依据列名删掉重复数据,inplace=True返回None,默认返回删除数据

7,返回缺失值数量和定位(配合any())

df.isnull().values.sum() #返回缺失值数量
df.isnull().any(axis=0) # 查看各列(axis=0)、行(axis=1)是否存在空值,True表示有空值
df[df.isnull().any(axis=1)] # 返回行为空的行

1117,返回缺失值数量

df.isnull().values.sum()

numpy

1,

matplotlib

1,样式美化plt.style.use()

2,fig, ax = plt.subplots(figsize=(16, 9))

3,ax.axhline(y=0, color=“red”, lw=2) 画出水平参考线,lw宽度

4,ax.plot(log_change, “.-”) “.-” 表示折线图中变化点画出点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值