Python基础+进阶
python基础:
#单行注释
''' 多
行
注
释
'''
Input: "A,B,C,D".split("," )
Output: ['A','B','C']
Input: "py".center(10,"-") Output: ----py----
Input: "{}是{}个{}的例子".format("这",1,"格式化") Output: 这是1个格式化的例子
if <条件1>:
<语句块1>
elif <条件2>:
<语句块2>
……
else:
<语句块>
循环结构:
for循环
for <循环变量> in <遍历结构>:
<语句块>
for i in range(N):
#range(N) :从0到N-1 表示循环N次
可用于遍历字符串,列表,文件
while循环
while <条件判断>:
<语句块>
保留字:break continue
break 跳出并结束当前整个循环,执行循环后语句。
continue 结束当次循环,继续执行后续次数循环。
定义函数:
def <函数名>(<参数>)
<函数体>
return <返回值>
参数:
实参,形参,位置参数,默认参数,可变参数*(将任意个参数组成元组),关键字参数**(将任意个参数组成字典)
class 类名(): def __init__(self, ): #初始化 … … def<函数名>() : <其他功能>
Input:a = np.array(range(10)) a<3
Output: array([ True, True, True, False, False, False, False, False, False,False])
#csv df = pd.read_csv(路径)df.head() 读前几行 df.tail() 读后几行 DataFrame df.columns 列名 df.index 索引 筛选 df[df.数学 > 80] 复杂筛选 df[(df.语文>80)& (df.数学>80)& (df.英语>80)] 排序 df.sort_values(['数学','语文']) 访问元素
scores = { '姓名':['老','咸','鱼'], '语文':[2,3,3], '英语':[6,6,6], '数学':[9,9,9] } df = pd.DataFrame(scores.index='one','two','three') df.loc[one] #访问第one行 df.iloc[0] #访问第0行 df.ix()#合并了loc和iloc的功能df.values() 打印所有数组 df.数学.values 打印数学数组 df.数学.value_counts() 简单的统计 df[['数学','语文']] df.数学 都为提取 xxx 列 分类: 重点
def func(scores): if scores>=85: return '优秀!!' if scores>=60 and socres<85: return '优秀!' if scores<60: return '凉了!!!' df['数学咋样?'] = df.数学.map(func)df['new_scores'] = df.apply(lambda x : x.数学 +x.语文,axis = 1) 添加new_scores这一列 df.drop(['new_scores'],axis = 1) 删除new_scores这一列 pandas中的dataframe 的操作,很大一部分跟numpy中的二维数组的操作是近似的。 matplotlib绘图 线性图
import matplotlib as plt %matplotlib inline plt.plot(x,y,'o',color = '' ,lambel = '',marksize =,linewidth = ,markeredgecolor='',makeredgewidth=) plt.legend() #与lambel相连,为图例 #o 点 -- 虚线 -p 五边形 -h 六边形 -D 正方形 -d 菱形
#散点图
plt.scatter(x,y)
#柱状图
df.plot.bar()#密度图
df.plot.kde()#直方图
df.hist() 优质内容筛选与推荐>>