Home Article Practice python常用命令

python常用命令

2021-06-04 20:29  views:623  source:海是歌    

(1)打开csv文件
import pandas as pd
df=pd.read_csv(r'data/data.csv')
(2)dataframe index 重新排序
data=df.sort_index(axis=0,ascending=False)
(3)dataframe 按照某1列进行升序或者降序排列
data=df.sort(['date'],ascending=True升序,False降序)
(4)dataframe 的index重新从0开始
data=data.reset_index(drop=True)
(5)画横坐标是日期的图
import matplotlib.pyplot as plt
x=data['date']#日期是字符串形式
y=data['close price']
plt.plot_date(x,y)
(6)求标准差
import numpy as np
np.std
(7)下取整
import math
math.floor
上取整:math.ceil
(8)希尔伯特变换
from scipy import fftpack
hx= fftpack.hilbert(price)
(9)值排序
data.order()
(10)差分
data.diff(1)#1阶差分
dataframe 删除元素
data.drop(元素位置)
(11)嵌套的array处理方法
import itertools
a = [[1,2,3],[4,5,6], [7], [8,9]]
out = list(itertools.chain.from_iterable(a))
(12)dataframe修改列名
data.columns=['num','price']
(13)excel表导入以后有空行解决办法
import numpy as np
data= data.drop
(data.loc[np.isnan(data.name.values)].index)
(15)diff用法
1.是dataframe或者series格式,直接就用data.diff()
2.是list格式,先转换成转换成list格式
data=data.tolist() 然后dif=np.diff(data)
(16)dataframe中的日期type不是date格式,不能直接相加减,所以先转换成list格式
t=data.time.tolist()
date_time = datetime.datetime.strptime
(str(t),'%Y-%m-%d %H:%M:%S')
date_time=datetime.date
(date_time.year,date_time.month,date_time.day)
past= date_time - datetime.timedelta(days=n*365)
(17)符号化
np.sign
(18)字典的使用
label={'11':'TP','1-1':'FN','-11':'FP','-1-1':'TN'}
for i in range(len(data1)):
state=str(int(data1[i]))+str(int(data2[i]))
result.append(label[state])
(19)用plt画图的时候中文不显示的解决办法
from matplotlib.font_manager import FontProperties
font_set = FontProperties
(fname=r”c:windowsontssimsun.ttc”, size=15)
plt.title(u'中文', fontproperties=font_set)
(20)获取当前程序运行的时间
from time import time
time1=time()
time2=time()
print(time2-time1)
1pandas读取文件
(1)有header
filename='data.csv'
df=pd.read_csv(filename)
#(2)txt(noheader)
#指定列 分隔符:(\t)
filename='data.txt'
df=pd.read_csv(filename,sep='\t',
header=None,usecols=[0,1,3,5],names=['','','',''])
2.rename
#ridaid
df=df.rename(columns={'rid':'Rid','aid':'Aid'})
3.是否存在用isin函数
#paper_ids
df=df[df.Rid.isin(paper_ids,use_hashmap=True)]
4.去重
df=df.drop_duplicates()
5.对含有NaN的行的处理
(1)填充值
#全部填充0
df.fillna(0)
#单列填充
df['A']=df['A'].fillna(0)
(2)删除这行
df=df.dropna()



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)