【发布时间】:2019-03-05 21:12:53
【问题描述】:
我有一个带有混合数据类型(dtypes)的 pandas 数据框,我希望将其转换为 numpy 结构化数组(或记录数组,在这种情况下基本相同)。对于纯数字数据帧,使用 to_records() 方法很容易做到这一点。我还需要将 pandas 列的 dtypes 转换为 strings 而不是 objects 以便我可以使用 numpy 方法 tofile() 将数字和字符串输出到二进制文件文件,但不会输出对象。
简而言之,我需要将带有 dtype=object 的 pandas 列转换为字符串或 unicode dtype 的 numpy 结构化数组。
这是一个示例,如果所有列都有数字(浮点数或整数)dtype,代码就足够了。
import pandas as pd
df=pd.DataFrame({'f_num': [1.,2.,3.], 'i_num':[1,2,3],
'char': ['a','bb','ccc'], 'mixed':['a','bb',1]})
struct_arr=df.to_records(index=False)
print('struct_arr',struct_arr.dtype,'\n')
# struct_arr (numpy.record, [('f_num', '<f8'), ('i_num', '<i8'),
# ('char', 'O'), ('mixed', 'O')])
但因为我想以字符串 dtype 结尾,所以我需要添加这个额外且有些涉及的代码:
lst=[]
for col in struct_arr.dtype.names: # this was the only iterator I
# could find for the column labels
dt=struct_arr[col].dtype
if dt == 'O': # this is 'O', meaning 'object'
# it appears an explicit string length is required
# so I calculate with pandas len & max methods
dt = 'U' + str( df[col].astype(str).str.len().max() )
lst.append((col,dt))
struct_arr = struct_arr.astype(lst)
print('struct_arr',struct_arr.dtype)
# struct_arr (numpy.record, [('f_num', '<f8'), ('i_num', '<i8'),
# ('char', '<U3'), ('mixed', '<U2')])
另请参阅:How to change the dtype of certain columns of a numpy recarray?
这似乎有效,因为字符和混合 dtypes 现在是 <U3 和 <U2 而不是 'O' 或 'object'。我只是在检查是否有更简单或更优雅的方法。但是由于 pandas 没有像 numpy 那样的原生字符串类型,也许没有?
【问题讨论】:
-
dt=df['mixed'].values.astype(str).dtype为我工作。 -
我很想蚕食
to_records,并结合您的 dtype 转换。它在列上进行迭代,并使用np.rec.fromarrays构建数组。 -
你看过那个函数的代码了吗?
-
我认为'cannibalize'更常用于机械,例如失事的飞机,而不是编程和功能。
-
@hpaulj 谢谢,这是一个很好的建议,我在我自己的问题的回答中加入了。 'cannibalize'也是一个很好的用法,我刚开始没有明白这个意思。 ;-)
标签: python arrays pandas numpy