Home > Article > Backend Development > What are the commonly used commands in python?
Commonly used commands in Python are: 1. Open csv file; 2. Reorder data [dataframe index]; 3. Find standard deviation; 4. Round up; 5. Hilbert transform; 6. Modify the column names of the dataframe; 7. Sort in ascending or descending order according to a certain column, etc.
[Recommended courses: Python Tutorial]
Commonly used commands in Python are:
(1) Open csv file
import pandas as pd df=pd.read_csv(r’data/data.csv’)
(2) Reorder dataframe index
data=df.sort_index(axis=0,ascending=False)
(3) Arrange the dataframe in ascending or descending order according to a certain column
data=df.sort([‘date’],ascending=True升序,False降序)
(4) The index of the dataframe starts from 0 again
data=data.reset_index(drop=True)
(5) Draw a picture with the date as the abscissa
import matplotlib.pyplot as plt x=data[‘date’]#日期是字符串形式 y=data[‘close price’] plt.plot_date(x,y)
(6) Find the standard deviation
import numpy as np np.std
(7) Round down
import math math.floor
Round up: math.ceil
(8) Hill Burt transform
from scipy import fftpack hx= fftpack.hilbert(price)
(9) Value sorting
data.order()
(10) Difference
data.diff(1)#一阶差分 dataframe 删除元素 data.drop(元素位置)
(11) Nested array processing method
import itertools a = [[1,2,3],[4,5,6], [7], [8,9]] out = list(itertools.chain.from_iterable(a))
( 12) Modify column names of dataframe
data.columns=[‘num’,’price’]
(13) Solution to empty rows after importing excel table
import numpy as np data= data.drop(data.loc[np.isnan(data.name.values)].index)
(15) Diff usage
1. It is dataframe or series format , just use data.diff() directly
2. It is in list format, first convert it into list format data=data.tolist() and then dif=np.diff(data)
(16) The date type in the dataframe is not in date format and cannot be added or subtracted directly, so it is converted to list format first
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) Symbolization
np.sign
(18) Use of dictionary
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) Solution to the problem that Chinese characters are not displayed when drawing with plt
from matplotlib.font_manager import FontProperties font_set = FontProperties(fname=r”c:\windows\fonts\simsun.ttc”, size=15) plt.title(u’中文’, fontproperties=font_set)
(20) Get the running time of the current program
from time import time time1=time() time2=time() print(time2-time1)
Summary: That’s it for this article That’s all, I hope it’s helpful to everyone.
The above is the detailed content of What are the commonly used commands in python?. For more information, please follow other related articles on the PHP Chinese website!