- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个这样的数据框fulldb_accrep_united
:
SparkID ... Period
0 913955 ... {"@PeriodName": "2000", "@DateBegin": "2000-01...
1 913955 ... {"@PeriodName": "1999", "@DateBegin": "1999-01...
2 16768 ... {"@PeriodName": "2007", "@DateBegin": "2007-01...
3 16768 ... {"@PeriodName": "2006", "@DateBegin": "2006-01...
4 16768 ... {"@PeriodName": "2005", "@DateBegin": "2005-01...
我需要将 Period
列(现在是字符串列)转换为 json
值列。通常我用 df.apply(lambda x: json.loads(x))
来做,但是这个数据框太大了,无法作为一个整体来处理。我想使用 dask
,但我似乎错过了一些重要的东西。我想我不明白如何在 dask
中使用 apply
,但我找不到解决方案。
代码
如果使用内存中所有 df 的 Pandas,我应该这样做:
#%% read df
os.chdir('/opt/data/.../download finance/output')
fulldb_accrep_united = pd.read_csv('fulldb_accrep_first_download_raw_quotes_corrected.csv', index_col = 0, encoding = 'utf-8')
os.chdir('..')
#%% Deleting some freaky symbols from column
condition = fulldb_accrep_united['Period'].str.contains('\\xa0', na = False, regex = False)
fulldb_accrep_united.loc[condition.values, 'Period'] = fulldb_accrep_united.loc[condition.values, 'Period'].str.replace('\\xa0', ' ', regex = False).values
#%% Convert to json
fulldb_accrep_united.loc[fulldb_accrep_united['Period'].notnull(), 'Period'] = fulldb_accrep_united['Period'].dropna().apply(lambda x: json.loads(x))
这是我尝试使用 dask
的代码:
#%% load data with dask
os.chdir('/opt/data/.../download finance/output')
fulldb_accrep_united = dd.read_csv('fulldb_accrep_first_download_raw_quotes_corrected.csv', encoding = 'utf-8', blocksize = 16 * 1024 * 1024) #16Mb chunks
os.chdir('..')
#%% setup calculation graph. No work is done here.
def transform_to_json(df):
condition = df['Period'].str.contains('\\xa0', na = False, regex = False)
df['Period'] = df['Period'].mask(condition.values, df['Period'][condition.values].str.replace('\\xa0', ' ', regex = False).values)
condition2 = df['Period'].notnull()
df['Period'] = df['Period'].mask(condition2.values, df['Period'].dropna().apply(lambda x: json.loads(x)).values)
result = transform_to_json(fulldb_accrep_united)
这里的最后一个单元格给出了错误:
NotImplementedError: Series getitem in only supported for other series objects with matching partition structure
我做错了什么?我花了近 5 个小时试图找到类似的主题,但我认为我遗漏了一些重要的东西,因为我是这个主题的新手。
最佳答案
你的问题太长了,我没看完。我很抱歉。参见 https://stackoverflow.com/help/minimal-reproducible-example
但是,根据标题,您可能希望将 json.loads 函数应用于数据框列中的每个元素
df["column-name"] = df["column-name"].apply(json.loads)
关于python - 使用 dask 为一列数据帧应用 json.loads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61919178/
如果我有一个依赖于某些全局或其他常量的函数,如下所示: x = 123 def f(partition): return partition + x # note that x is def
我们可以通过哪些方式在 Dask Arrays 中执行项目分配?即使是一个非常简单的项目分配,如:a[0] = 2 不起作用。 最佳答案 正确的。这是文档中提到的第一个限制。 通常,涉及 for 循环
[mapr@impetus-i0057 latest_code_deepak]$ dask-worker 172.26.32.37:8786 distributed.nanny - INFO -
我正在构建一个 FastAPI 应用程序,它将为 Dask 数组的 block 提供服务。我想利用 FastAPI's asynchronous functionality旁边Dask-distrib
在延迟数据帧处理的几个阶段之后,我需要在保存数据帧之前对其进行重新分区。但是,.repartition() 方法要求我知道分区的数量(而不是分区的大小),这取决于处理后数据的大小,这是未知的。 我想我
我正在努力转换 dask.bag将字典放入 dask.delayed pandas.DataFrames进入决赛 dask.dataframe 我有一个函数 (make_dict) 将文件读入一个相当
我正在尝试使用 dask_cudf/dask 读取单个大型 parquet 文件(大小 > gpu_size),但它目前正在读取它到一个分区中,我猜这是从文档字符串推断出的预期行为: dask.dat
当启动一个 dask 分布式本地集群时,您可以为 dashboard_address 设置一个随机端口或地址。 如果稍后获取scheduler对象。有没有办法提取仪表板的地址。 我有这个: clust
我有一个 dask 数据框,由 parquet 支持。它有 1.31 亿行,当我对整个帧执行一些基本操作时,它们需要几分钟。 df = dd.read_parquet('data_*.pqt') un
我正在使用 24 个 vCPU 的谷歌云计算实例。运行代码如下 import dask.dataframe as dd from distributed import Client client =
我正在尝试在多台机器上分发一个大型 Dask 数据帧,以便(稍后)在数据帧上进行分布式计算。我为此使用了 dask-distributed。 我看到的所有 dask 分布式示例/文档都是从网络资源(h
我在 Django 服务器后面使用 Dask,这里总结了我的基本设置:https://github.com/MoonVision/django-dask-demo/可以在这里找到 Dask 客户端:h
我有以下格式的 Dask DataFrame: date hour device param value 20190701 21 dev_01 att_1 0.00
我正在尝试使用 dask 而不是 Pandas,因为我有 2.6gb csv 文件。 我加载它,我想删除一列。但似乎无论是 drop 方法 df.drop('column') 或切片 df[ : ,
我有一个比我的内存大得多的文本文件。我想按字典顺序对该文件的行进行排序。我知道如何手动完成: 分成适合内存的块 对块进行排序 合并块 我想用 dask 来做。我认为处理大量数据将是 dask 的一个用
使用 Dask 的分布式调度程序时,我有一个正在远程工作人员上运行的任务,我想停止该任务。 我该如何阻止?我知道取消方法,但如果任务已经开始执行,这似乎不起作用。 最佳答案 如果它还没有运行 如果任务
我需要将一个非常大的 dask.bag 的元素提交到一个非线程安全的存储区,即我需要类似的东西 for x in dbag: store.add(x) 我无法使用compute,因为包太大,无
如果我有一个已经索引的 Dask 数据框 >>> A.divisions (None, None) >>> A.npartitions 1 我想设置分区,到目前为止我正在做 A.reset_index
根据 this回答,如果 Dask 知道数据帧的索引已排序,则 Dask 数据帧可以执行智能索引。 如果索引已排序,我如何让 Dask 知道? 在我的具体情况下,我正在做这样的事情: for sour
我想从具有特定数量的工作人员的 python 启动本地集群,然后将客户端连接到它。 cluster = LocalCluster(n_workers=8, ip='127.0.0.1') client
我是一名优秀的程序员,十分优秀!