程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pandas common methods

編輯:Python
DataFrame Get column index name df.columns Get row index name df.index Redefine the column index df.columns=["X","Y","Z"] Change the column name of a column again ( If there is a lack of inplace Options , Will not change , Instead, add new columns .)df.rename(columns={'x':'X'},inplace=True) Print DataFrame Attribute information df.info() see DataFrame Data information of the first five lines df.head() see DataFrame The last five lines of data information df.tail() Create an empty data frame , Easy to add data later
df_empty = pd.DataFrame(columns=['x', 'y', 'z'])

Add data to an empty data box
df_new = pd.DataFrame(np.arange(6).reshape(2, 3), columns=['x', 'y', 'z'])
print(pd.concat([df_empty, df_new], axis=0))
Add a new row to the existing data frame / Column , Or horizontal / Add another table vertically (axis Indicates along the longitudinal axis (axis=0) Or horizontal axis (axis=1) Directional connection )

dat_cbind = pd.concat([df1, df2], axis=1)

dat_rbind = pd.concat([df1, df3], axis=0)

dat_rbind = df1.append(df3)

Delete rows or columns ( among ,labels Is the row to delete / Column name , Give... With a list ;axis The default is 0, Refers to the deletion of a line , So delete columns When it comes to Appoint axis=1;index Directly specify the row to delete ;columns Directly specify the column to be deleted ;inplace=False, By default, the deletion operation is not Change the original data , Instead, it returns a new... After the deletion operation DataFrame;inplace=True, The original data will be deleted directly operation , And cannot return after deletion )

df1.drop(labels='y', axis=1, inplace=True)

apply The object of operation is DataFrame A column of (axis=0) Or a certain line (axis=1)applymap The operation object is element level , Act on each DataFrame Every data of map The operation object is also element level , But it is right Series Call the function once for each data in the Sum up by line df_rowsum = df[['2020', '2021']].apply(lambda x: x.sum(), axis=1) Sum by column

df_colsum = df[['2020', '2021']].apply(lambda x: x.sum(), axis=0)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved