我需要创建一个非常大的numpy数组来保存非负整数值。我事先知道最大的整数是多少,所以我想尝试使用尽可能小的数据类型。到目前为止我有以下内容:
>>> import numpy as np
>>> def minimal_type(max_val, types=[np.uint8,np.uint16,np.uint32,np.uint64]):
''' finds the minimal data type needed to correctly store the given max_val
returns None if none of the provided types are sufficient
'''
for t in types:
if max_val <= np.iinfo(t).max:
return t
return None
>>> print(minimal_type(42))
<class 'numpy.uint8'>
>>> print(minimal_type(255))
<class 'numpy.uint8'>
>>> print(minimal_type(256))
<class 'numpy.uint16'>
>>> print(minimal_type(4200000000))
<class 'numpy.uint32'>
>>>
Run Code Online (Sandbox Code Playgroud)
有没有numpy内置的方法来实现此功能?
我有以下代码,我正在尝试将数据框写入 Excel 文件的“现有”工作表(此处称为 test.xlsx)。Sheet3 是我要放置数据的目标工作表,我不想用新工作表替换整个工作表。
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
book = load_workbook('test.xlsx')
writer = pd.ExcelWriter('test.xlsx')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets) # *I am not sure what is happening in this line*
df.to_excel(writer,"Sheet3",startcol=0, startrow=20)
Run Code Online (Sandbox Code Playgroud)
当我逐行运行代码时,最后一行出现此错误:
AttributeError: 'Workbook' 对象没有属性 'add_worksheet'。现在为什么当我不尝试添加工作表时会看到此错误?
注意:我知道这个类似的问题Python How to use ExcelWriter to write into an existing worksheet但它对我不起作用,我也无法评论该帖子。