我有一个非常大的netCDF文件,我正在使用python中的netCDF4阅读
我无法一次读取此文件,因为它的尺寸(1200 x 720 x 1440)太大,整个文件不能同时在内存中.第一维代表时间,下一个分别代表纬度和经度.
import netCDF4
nc_file = netCDF4.Dataset(path_file, 'r', format='NETCDF4')
for yr in years:
nc_file.variables[variable_name][int(yr), :, :]
Run Code Online (Sandbox Code Playgroud)
然而,一次阅读一年是非常缓慢的.如何加快以下用例的速度?
- 编辑
chunksize是1
我可以阅读一系列年份:nc_file.variables [variable_name] [0:100,:,]
有几个用例:
多年来:
numpy.ma.sum(nc_file.variables[variable_name][int(yr), :, :])
Run Code Online (Sandbox Code Playgroud)# Multiply each year by a 2D array of shape (720 x 1440)
for yr in years:
numpy.ma.sum(nc_file.variables[variable_name][int(yr), :, :] * arr_2d)
Run Code Online (Sandbox Code Playgroud)
# Add 2 netcdf files together
for yr in years:
numpy.ma.sum(nc_file.variables[variable_name][int(yr), :, :] +
nc_file2.variables[variable_name][int(yr), :, :])
Run Code Online (Sandbox Code Playgroud) 我有一个 DataFrame,其中包含一个列,其中每个单元格都由一个 dicts 列表组成,并且每个 dicts 列表的长度不同(包括 0)。
一个例子:
df = pd.DataFrame({'ID' : [13423,294847,322844,429847], 'RANKS': [[{u'name': u'A', u'price': u'$1.00', u'rank': u'1'},
{u'name': u'B', u'price': u'$4.00', u'rank': u'2'},
{u'name': u'C', u'price': u'$3.99', u'rank': u'3'},
{u'name': u'D', u'price': u'$2.00', u'rank': u'4'},
{u'name': u'E', u'price': u'$2.50', u'rank': u'5'}],
[{u'name': u'AA', u'price': u'$1.99', u'rank': u'1'},
{u'name': u'BB', u'price': u'$6.99', u'rank': u'2'}],
[{u'name': u'Z', u'price': u'$0.99', u'rank': u'1'},
{u'name': u'Y', u'price': u'$10.00', u'rank': u'2'},
{u'name': u'X', u'price': u'$1.99', u'rank': u'3'}],[]], 'count' : [5,2,3,0]})
Run Code Online (Sandbox Code Playgroud)
请注意,“count”是“RANKS”中的字典数。我的目标是创建一系列额外的数据帧/表(每个“等级”一个)并将它们链接到 HDFStore 中的主表。就像是: …
我有一个pandas数据帧:
apple banana carrot diet coke
1 1 1 0
0 1 0 0
1 0 0 0
1 0 1 1
0 1 1 0
0 1 1 0
Run Code Online (Sandbox Code Playgroud)
我想将此转换为以下内容:
[['apple', 'banana', 'carrot'],
['banana'],
['apple'],
['apple', 'carrot', 'diet coke'],
['banana', 'carrot'],
['banana', 'carrot']]
Run Code Online (Sandbox Code Playgroud)
我该怎么做?非常感谢.
从文档:
sys.getrecursionlimit()返回递归限制的当前值,即Python解释器堆栈的最大深度.此限制可防止无限递归导致C堆栈溢出并导致Python崩溃.它可以通过setrecursionlimit()设置.
我正在腌制一个对象时达到递归限制.我正在腌制的物体只有几层嵌套,所以我对发生的事情感到有些困惑.
我已经能够通过以下黑客来规避这个问题:
try:
return pickle.dumps(x)
except:
try:
recursionlimit = getrecursionlimit()
setrecursionlimit(2*recursionlimit)
dumped = pickle.dumps(x)
setrecursionlimit(recursionlimit)
return dumped
except:
raise
Run Code Online (Sandbox Code Playgroud)
在不同的上下文中测试上述片段有时会导致第一个片段成功try,有时它会导致第二个片段成功try.到目前为止,我还没有把它raise作为例外.
为了进一步调试我的问题,有一种方法可以获得堆栈的当前深度.这将允许我验证进入的堆栈深度是否确定上面的片段是否将在第一个try或第二个上成功.
标准库是否提供了获取堆栈深度的函数,如果没有,我该如何获取它?
def get_stack_depth():
# what goes here?
Run Code Online (Sandbox Code Playgroud) 我有一个数据帧,我需要根据以下条件过滤它
CITY == 'Mumbai' & LANGUAGE == 'English' & GENRE == 'ACTION' & count_GENRE >= 1
CITY == 'Mumbai' & LANGUAGE == 'English' & GENRE == 'ROMANCE' & count_GENRE >= 1
CITY == 'Mumbai' & LANGUAGE == 'Hindi' & count_LANGUAGE >= 1 & GENRE == 'ACTION'
Run Code Online (Sandbox Code Playgroud)
当我试图这样做的时候
df1 = df.query(condition1)
df2 = df.query(condition2)
Run Code Online (Sandbox Code Playgroud)
我收到内存错误(因为我的数据框大小是巨大的).
所以我计划通过过滤主要条件然后子条件,这样负载会更少,性能会更好.
通过解析上述条件,以某种方式设法获得
main_filter = "CITY == 'Mumbai'"
sub_cond1 = "LANGUAGE == 'English'"
sub_cond1_cond1 = "GENRE == 'ACTION' & count_GENRE >= 1"
sub_cond1_cond2 = "GENRE == 'ROMANCE' …Run Code Online (Sandbox Code Playgroud) 我有一个包含 26 列的 CSV 文件。我正在尝试查找某些文本,以便获得我需要的信息。两列是“姓氏”、“名字”。
我在这里找到了这段代码,但我没有得到我需要的东西。
dframe['First Name'].where(dframe['Last Name']="James","Turner")
Run Code Online (Sandbox Code Playgroud)
我收到错误:SyntaxError:关键字不能是表达式
然后我只尝试了名字:
dframe['Last Name']=="Turner"
Run Code Online (Sandbox Code Playgroud)
我得到 659.000 行的布尔值 False。
pandas ×4
python ×4
dask ×1
dataframe ×1
hdfstore ×1
interpreter ×1
netcdf ×1
nodes ×1
numpy ×1
pytables ×1
python-2.7 ×1
python-3.x ×1
stack ×1
traversal ×1