Home > Backend Development > Python Tutorial > What are the commonly used commands in python?

What are the commonly used commands in python?

清浅
Release: 2020-09-08 10:48:40
Original
26734 people have browsed it

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.

What are the commonly used commands in python?

[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’)
Copy after login

(2) Reorder dataframe index

data=df.sort_index(axis=0,ascending=False)
Copy after login

(3) Arrange the dataframe in ascending or descending order according to a certain column

data=df.sort([‘date’],ascending=True升序,False降序)
Copy after login

(4) The index of the dataframe starts from 0 again

data=data.reset_index(drop=True)
Copy after login

(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)
Copy after login

(6) Find the standard deviation

import numpy as np 
np.std
Copy after login

(7) Round down

import math 
math.floor
Copy after login

Round up: math.ceil

(8) Hill Burt transform

from scipy import fftpack 
hx= fftpack.hilbert(price)
Copy after login

(9) Value sorting

data.order()
Copy after login

(10) Difference

data.diff(1)#一阶差分
dataframe 删除元素 
data.drop(元素位置)
Copy after login

(11) Nested array processing method

import itertools 
a = [[1,2,3],[4,5,6], [7], [8,9]] 
out = list(itertools.chain.from_iterable(a))
Copy after login

( 12) Modify column names of dataframe

data.columns=[‘num’,’price’]
Copy after login

(13) Solution to empty rows after importing excel table

import numpy as np 
data= data.drop(data.loc[np.isnan(data.name.values)].index)
Copy after login

(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)
Copy after login

(17) Symbolization

np.sign
Copy after login

(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])
Copy after login

(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)
Copy after login

(20) Get the running time of the current program

from time import time 
time1=time() 
time2=time() 
print(time2-time1)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template