我在想是否有一种优雅简洁的方法可以在 Pandas 数据框中按数据类型 (dtype) 选择列。也就是说,只从 DataFrame 中选择 int64 列。
更具体地说,类似于:
更具体地说,类似于:
df.select_columns(dtype=float64)
select_dtypes
方法,因此您可以更加优雅/通用地执行此操作。In [11]: df = pd.DataFrame([[1, 2.2, 'three']], columns=['A', 'B', 'C'])
In [12]: df.select_dtypes(include=['int'])
Out[12]:
A
0 1
选择所有数值类型,请使用numpy数据类型numpy.number。In [13]: df.select_dtypes(include=[np.number])
Out[13]:
A B
0 1 2.2
In [14]: df.select_dtypes(exclude=[object])
Out[14]:
A B
0 1 2.2
df.loc[:, df.dtypes == np.float64]
df[df.dtypes == np.float64]
不起作用?非常感谢!希望这对理解pandas有所帮助。 - Ray Walkerdf.select_dtypes(include=[np.float64])
我想通过添加选项来扩展现有答案,以便选择所有浮点数据类型或所有整数数据类型:
演示:
np.random.seed(1234)
df = pd.DataFrame({
'a':np.random.rand(3),
'b':np.random.rand(3).astype('float32'),
'c':np.random.randint(10,size=(3)).astype('int16'),
'd':np.arange(3).astype('int32'),
'e':np.random.randint(10**7,size=(3)).astype('int64'),
'f':np.random.choice([True, False], 3),
'g':pd.date_range('2000-01-01', periods=3)
})
产生的结果是:In [2]: df
Out[2]:
a b c d e f g
0 0.191519 0.785359 6 0 7578569 False 2000-01-01
1 0.622109 0.779976 8 1 7981439 True 2000-01-02
2 0.437728 0.272593 0 2 2558462 True 2000-01-03
In [3]: df.dtypes
Out[3]:
a float64
b float32
c int16
d int32
e int64
f bool
g datetime64[ns]
dtype: object
选择所有浮点数列:
In [4]: df.select_dtypes(include=['floating'])
Out[4]:
a b
0 0.191519 0.785359
1 0.622109 0.779976
2 0.437728 0.272593
In [5]: df.select_dtypes(include=['floating']).dtypes
Out[5]:
a float64
b float32
dtype: object
选择所有整数列:
In [6]: df.select_dtypes(include=['integer'])
Out[6]:
c d e
0 6 0 7578569
1 8 1 7981439
2 0 2 2558462
In [7]: df.select_dtypes(include=['integer']).dtypes
Out[7]:
c int16
d int32
e int64
dtype: object
选择所有数值列:
In [8]: df.select_dtypes(include=['number'])
Out[8]:
a b c d e
0 0.191519 0.785359 6 0 7578569
1 0.622109 0.779976 8 1 7981439
2 0.437728 0.272593 0 2 2558462
In [9]: df.select_dtypes(include=['number']).dtypes
Out[9]:
a float64
b float32
c int16
d int32
e int64
dtype: object
使用多个 include 选项选择列的类型,例如 float64 和 int64。
df_numeric = df.select_dtypes(include=[np.float64,np.int64])
select_dtypes(include=[np.int])
如果你想选择int64列并进行“原地”更新,可以使用以下方法:
int64_cols = [col for col in df.columns if is_int64_dtype(df[col].dtype)]
df[int64_cols]
In [1]:
import pandas as pd
from pandas.api.types import is_int64_dtype
df = pd.DataFrame({'a': [1, 2] * 3,
'b': [True, False] * 3,
'c': [1.0, 2.0] * 3,
'd': ['red','blue'] * 3,
'e': pd.Series(['red','blue'] * 3, dtype="category"),
'f': pd.Series([1, 2] * 3, dtype="int64")})
int64_cols = [col for col in df.columns if is_int64_dtype(df[col].dtype)]
print('int64 Cols: ',int64_cols)
print(df[int64_cols])
df[int64_cols] = 0
print(df[int64_cols])
Out [1]:
int64 Cols: ['a', 'f']
a f
0 1 1
1 2 2
2 1 1
3 2 2
4 1 1
5 2 2
a f
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
仅供完整性参考:
df.loc() 和 df.select_dtypes() 将从数据框中给出一个切片的副本。这意味着如果你尝试使用 df.select_dtypes() 更新值,你会得到一个 SettingWithCopyWarning 警告,并且 df 中的更新不会发生。
例如,请注意当我尝试使用 .loc() 或 .select_dtypes() 选择列来更新 df 时,没有任何变化:
In [2]:
df = pd.DataFrame({'a': [1, 2] * 3,
'b': [True, False] * 3,
'c': [1.0, 2.0] * 3,
'd': ['red','blue'] * 3,
'e': pd.Series(['red','blue'] * 3, dtype="category"),
'f': pd.Series([1, 2] * 3, dtype="int64")})
df_bool = df.select_dtypes(include='bool')
df_bool.b[0] = False
print(df_bool.b[0])
print(df.b[0])
df.loc[:, df.dtypes == np.int64].a[0]=7
print(df.a[0])
Out [2]:
False
True
1
如果你不想在过程中创建数据框的子集,可以直接迭代列数据类型。
我还没有对下面的代码进行基准测试,假设如果你处理非常大的数据集,它将会更快。
[col for col in df.columns.tolist() if df[col].dtype not in ['object','<M8[ns]']]
你可以使用:
for i in x.columns[x.dtypes == 'object']:
print(i)
如果您只想显示特定数据框的列名而不是切片数据框,请使用以下代码。不确定Python是否有此类函数。
PS:将object
替换为所需的数据类型。