标签:tps color line ... osi set 相关 training 方法
Pandas可根据列名称选取,还可以根据列所在的position(数字,在第几行第几列,注意pandas行列的position是从0开始)选取。相关函数如下:
1)loc,基于列label,可选取特定行(根据行index);
2)iloc,基于行/列的position;
3)at,根据指定行index及列label,快速定位DataFrame的元素;
4)iat,与at类似,不同的是根据position来定位的;
5)ix,为loc与iloc的混合体,既支持label也支持position;
The simplified rules of indexing are
- Use
loc
for label-based indexing
- Use
iloc
for positional indexing
-
-
-
-
df = pd.read_csv(‘./iris_training.csv‘, low_memory=False)
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
-
-
-
行选择
Pandas进行行选择一般有三种方法:
- 连续多行的选择用类似于python的列表切片
- loc通过行标签索引来确定行的
- iloc通过行号索引来确定行
-
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Name: 0, dtype: float64 """
-
-
-
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
df2 = df.drop([1,2], axis=0)
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Passing list-likes to .loc or [] with any missing label will raise
-
KeyError in the future, you can use .reindex() as an alternative.
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
120 4 setosa versicolor virginica
-
-
-
-
-
-
-
-
120 4 setosa versicolor virginica
-
-
列选择
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Name: 120, Length: 120, dtype: float64"""
-
-
-
print(df[[‘120‘, ‘setosa‘]])
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
[120 rows x 2 columns] """
-
-
-
行列选择
-
-
-
-
-
print(df.loc[1:3, [‘120‘, ‘setosa‘]])
-
-
-
-
-
-
-
print(df.loc[1:3, ‘120‘: ‘setosa‘])
-
-
-
-
-
-
-
print(df.iloc[1:3, [1, 2]])
-
-
-
-
-
-
-
-
-
-
总结
1).loc,.iloc,.ix,只加第一个参数如.loc([1,2]),.iloc([2:3]),.ix[2]…则进行的是行选择
2).loc,.at,选列是只能是列名,不能是position
3).iloc,.iat,选列是只能是position,不能是列名
4)df[]只能进行行选择,或列选择,不能同时进行列选择,列选择只能是列名。
pandas的DataFrame的行列选择
标签:tps color line ... osi set 相关 training 方法
原文地址:https://www.cnblogs.com/liangzaikaituozhe/p/10337286.html