[英]changing numpy array to float
I have a numpy array of type object. 我有一个类型对象的numpy数组。 I want to find the columns with numerical values and cast them to float.
我想找到具有数值的列并将其强制转换为浮点型。 Also I want to find the indices of the columns with object values.
我也想找到带有对象值的列的索引。 this is my attempt:
这是我的尝试:
import numpy as np
import pandas as pd
df = pd.DataFrame({'A' : [1,2,3,4,5],'B' : ['A', 'A', 'C', 'D','B']})
X = df.values.copy()
obj_ind = []
for ind in range(X.shape[1]):
try:
X[:,ind] = X[:,ind].astype(np.float32)
except:
obj_ind = np.append(obj_ind,ind)
print obj_ind
print X.dtype
and this is the output I get: 这是我得到的输出:
[ 1.]
object
Generally your idea of trying to apply astype
to each column is fine. 通常,您尝试将
astype
应用于每个列的想法都很好。
In [590]: X[:,0].astype(int)
Out[590]: array([1, 2, 3, 4, 5])
But you have to collect the results in a separate list. 但是您必须将结果收集在单独的列表中。 You can't just put them back in
X
. 您不能只是将它们放回
X
。 That list can then be concatenated. 然后可以将该列表连接起来。
In [601]: numlist=[]; obj_ind=[]
In [602]: for ind in range(X.shape[1]):
.....: try:
.....: x = X[:,ind].astype(np.float32)
.....: numlist.append(x)
.....: except:
.....: obj_ind.append(ind)
In [603]: numlist
Out[603]: [array([ 3., 4., 5., 6., 7.], dtype=float32)]
In [604]: np.column_stack(numlist)
Out[604]:
array([[ 3.],
[ 4.],
[ 5.],
[ 6.],
[ 7.]], dtype=float32)
In [606]: obj_ind
Out[606]: [1]
X
is a numpy array with dtype object
: X
是具有dtype object
的numpy数组:
In [582]: X
Out[582]:
array([[1, 'A'],
[2, 'A'],
[3, 'C'],
[4, 'D'],
[5, 'B']], dtype=object)
You could use the same conversion logic to create a structured array with a mix of int and object fields. 您可以使用相同的转换逻辑来创建包含int和object字段的结构化数组。
In [616]: ytype=[]
In [617]: for ind in range(X.shape[1]):
try:
x = X[:,ind].astype(np.float32)
ytype.append('i4')
except:
ytype.append('O')
In [618]: ytype
Out[618]: ['i4', 'O']
In [620]: Y=np.zeros(X.shape[0],dtype=','.join(ytype))
In [621]: for i in range(X.shape[1]):
Y[Y.dtype.names[i]] = X[:,i]
In [622]: Y
Out[622]:
array([(3, 'A'), (4, 'A'), (5, 'C'), (6, 'D'), (7, 'B')],
dtype=[('f0', '<i4'), ('f1', 'O')])
Y['f0']
gives the the numeric field. Y['f0']
给出数字字段。
df.dtypes
return a pandas series which can be operated further df.dtypes
返回一个熊猫系列,可以进一步操作
# find columns of type int
mask = df.dtypes==int
# select columns for for the same
cols = df.dtypes[mask].index
# select these columns and convert to float
new_cols_df = df[cols].apply(lambda x: x.astype(float), axis=1)
# Replace these columns in original df
df[new_cols_df.columns] = new_cols_df
I think this might help 我认为这可能会有所帮助
def func(x):
a = None
try:
a = x.astype(float)
except:
# x.name represents the current index value
# which is column name in this case
obj.append(x.name)
a = x
return a
obj = []
new_df = df.apply(func, axis=0)
This will keep the object
columns as such which you can use later. 这样将保留
object
列,以供日后使用。
Note : While using pandas.DataFrame
avoid using iteration using loop as this much slower than performing the same operation using apply
. 注意 :在使用
pandas.DataFrame
避免使用循环进行迭代,因为这比使用apply
执行相同的操作要慢得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.