Pandas - the difference between ix and loc and iloc and icol

From: https://blog.csdn.net/xw_classmate/article/details/51333646

From: https://blog.csdn.net/chenKFKevin/article/details/62049060

From: https://blog.csdn.net/xiaodongxiexie/article/details/53108959

1. It should be noted that if df = df.loc[0:2, ['A', 'C']] or df = df.loc[0:2, ['A', 'C']] , after slicing, the type is still dataframe, and operations such as addition, subtraction, multiplication and division cannot be performed directly. For example, one column of the dataframe is the math score (shuxue), and the other column is the language score (yuwen). Now you need to find the sum of the two courses. You can use df['shuxue'] + df['yuwen'] (the type is series after selection) to get the total score, but not df.iloc[:,[2]]+df.iloc[:,[1 ]] or df.iloc[:,['shuxue']]+df.iloc[:,['yuwen']], which would produce incorrect results.

2. Another way is to use df.icol(i) to select a column, which is not a dataframe but a series, i is the position of the column, and counts from 0.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325095348&siteId=291194637