pandas基本操作 Posted on 2019-09-18 | Edited on 2020-04-20 | In python 1. pandas DataFrame对象的创建12dates = pd.date_range('20130101', periods=6)df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD')) 2. 操作数据12345678910111213141516171819202122232425262728293031323334353637df.head()df.tail(3)df.indexdf.describe()# 单列索引df.iloc[1:2,2:]# 特定索引df.iloc[[1,2],[2,3]]# 布尔索引df[df>0.5] = 1# 赋值列df2['E'] = ['A,','B','C','E','3','3']# 赋值行df2.loc['e'] = [1,2,3,4,5]#访问某个位置df2.iat[0,1]#删除带有缺失值的行df1.dropna(how = 'any')#填充空数据df1.fillna(value=5)# 获得每一列的平均值、df1.mean()# 获得每一行的平均值df1.mean(1)# 修改行标签df1.index = ['A','B','C']#修改行列data.rename(index={'A':'D', 'B':'E', 'C':'F'}, columns={'a':'d','b':'e','c':'f'}, inplace = True)#连接两个表pe = [df1,df2]pd.concat(pe)#数据库连接风格left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})pd.merge(left, right, on='key')#写入文件df1.to_csv('foo.csv') Donate comment here Donate WeChat Pay