pandas DataFrame.drop 函数中的整数参数
Integer parameter in pandas DataFrame.drop function
在下面的代码行中
X = np.array(df.drop(['label'], 1))
您能解释一下数字 1
的作用吗?
从文档中我了解到 DataFrame.drop
函数从数据框中删除了名为 'label'
的所需列,并且 returns 没有此列的新数据框。但是我不明白这个特定的整数参数 1
是做什么的。
是drop
中的参数axis
。与axis=1
相同。这意味着您需要从 DataFrame
中删除在第一个参数 labels
:
中指定的列
labels
大多数时候被省略。
如果需要删除带有 index
的行,可以删除参数 axis
,因为默认情况下 axis=0
。
参数axis=1
有时会被1
代替,因为文字少,但可读性差。
样本:
import pandas as pd
df = pd.DataFrame({'label':[1,2,3],
'label1':[4,5,6],
'label2':[7,8,9]})
print (df)
label label1 label2
0 1 4 7
1 2 5 8
2 3 6 9
print (df.drop(['label'], 1))
label1 label2
0 4 7
1 5 8
2 6 9
#most commonly used
print (df.drop(['label'], axis=1))
label1 label2
0 4 7
1 5 8
2 6 9
print (df.drop(labels=['label'], axis=1))
label1 label2
0 4 7
1 5 8
2 6 9
您想删除名为 'age' 的数据,您必须指出它是一列。
因此,当您从数据类型
中删除 'age' 时
x = np.array(data.drop([predict], 1))
.
如果年龄是x轴
x = np.array(data.drop([predict], 0))
在下面的代码行中
X = np.array(df.drop(['label'], 1))
您能解释一下数字 1
的作用吗?
从文档中我了解到 DataFrame.drop
函数从数据框中删除了名为 'label'
的所需列,并且 returns 没有此列的新数据框。但是我不明白这个特定的整数参数 1
是做什么的。
是drop
中的参数axis
。与axis=1
相同。这意味着您需要从 DataFrame
中删除在第一个参数 labels
:
labels
大多数时候被省略。
如果需要删除带有 index
的行,可以删除参数 axis
,因为默认情况下 axis=0
。
参数axis=1
有时会被1
代替,因为文字少,但可读性差。
样本:
import pandas as pd
df = pd.DataFrame({'label':[1,2,3],
'label1':[4,5,6],
'label2':[7,8,9]})
print (df)
label label1 label2
0 1 4 7
1 2 5 8
2 3 6 9
print (df.drop(['label'], 1))
label1 label2
0 4 7
1 5 8
2 6 9
#most commonly used
print (df.drop(['label'], axis=1))
label1 label2
0 4 7
1 5 8
2 6 9
print (df.drop(labels=['label'], axis=1))
label1 label2
0 4 7
1 5 8
2 6 9
您想删除名为 'age' 的数据,您必须指出它是一列。
因此,当您从数据类型
中删除 'age' 时x = np.array(data.drop([predict], 1))
.
如果年龄是x轴
x = np.array(data.drop([predict], 0))