在 python pandas 中,shift 函数可用于将数据帧中的行向前移动,并且可能相对于原始行,从而允许计算时间序列数据的变化。Julia 中的等效方法是什么?
我有一个 csv 文件,如下所示,
\n20\xc3\x972 DataFrame\n\xe2\x94\x82 Row \xe2\x94\x82 Id \xe2\x94\x82 Date \xe2\x94\x82\n\xe2\x94\x82 \xe2\x94\x82 Int64 \xe2\x94\x82 String \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 1 \xe2\x94\x82 1 \xe2\x94\x82 01-01-2010 \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x82 2 \xe2\x94\x82 02-01-2010 \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x82 3 \xe2\x94\x82 03-01-2010 \xe2\x94\x82\n\xe2\x94\x82 4 \xe2\x94\x82 4 \xe2\x94\x82 04-01-2010 \xe2\x94\x82\n\xe2\x94\x82 5 \xe2\x94\x82 5 \xe2\x94\x82 05-01-2010 \xe2\x94\x82\n\xe2\x94\x82 6 \xe2\x94\x82 6 \xe2\x94\x82 06-01-2010 \xe2\x94\x82\n\xe2\x94\x82 7 \xe2\x94\x82 7 \xe2\x94\x82 07-01-2010 \xe2\x94\x82\n\xe2\x94\x82 8 \xe2\x94\x82 8 \xe2\x94\x82 08-01-2010 \xe2\x94\x82\n\xe2\x94\x82 9 \xe2\x94\x82 9 \xe2\x94\x82 09-01-2010 \xe2\x94\x82\n\xe2\x94\x82 10 \xe2\x94\x82 10 \xe2\x94\x82 10-01-2010 \xe2\x94\x82\n\xe2\x94\x82 11 \xe2\x94\x82 11 …Run Code Online (Sandbox Code Playgroud) 我有一个包含日期和股票价格的 Excel 文件。我使用 DataFrames.jl 将此数据读入数据帧
\nusing DataFrames, StatsPlots, Indicators\n\ndf = DataFrame(XLSX.readtable("Demo-sv.xlsx", "Blad3")...)\nRun Code Online (Sandbox Code Playgroud)\n这非常有效,我在这里打印了前 6 个条目。
\n6\xc3\x972 DataFrame\n\xe2\x94\x82 Row \xe2\x94\x82 Date \xe2\x94\x82 Closeprice \xe2\x94\x82\n\xe2\x94\x82 \xe2\x94\x82 Any \xe2\x94\x82 Any \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 1 \xe2\x94\x82 2019-05-03 \xe2\x94\x82 169.96 \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x82 2019-05-02 \xe2\x94\x82 168.06 \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x82 2019-04-30 \xe2\x94\x82 165.58 \xe2\x94\x82\n\xe2\x94\x82 4 \xe2\x94\x82 2019-04-29 \xe2\x94\x82 166.4 \xe2\x94\x82\n\xe2\x94\x82 5 \xe2\x94\x82 2019-04-26 \xe2\x94\x82 167.76 \xe2\x94\x82\n\xe2\x94\x82 6 \xe2\x94\x82 2019-04-25 \xe2\x94\x82 167.46 \xe2\x94\x82\nRun Code Online (Sandbox Code Playgroud)\n然后我使用 StatsPlots.jl 绘制这些数据@df df plot(df.Date, df.Closeprice)并得到一个漂亮的绘图。
问题是当我想用 Indicators.jl 绘制简单的移动平均线时 …
在 DataFrames.jl 中查找 DataFrame 中的行的规范方法是什么?
\n例如,给定这个 DataFrame:
\n\xe2\x94\x82 Row \xe2\x94\x82 uuid \xe2\x94\x82 name \n\xe2\x94\x82 \xe2\x94\x82 String \xe2\x94\x82 String \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\xe2\x94\x82 1 \xe2\x94\x82 0efae8bf-39e6-5d65-b05d-c8947f4cee2a \xe2\x94\x82 COSMA_jll \n\xe2\x94\x82 2 \xe2\x94\x82 17ccb2e5-db19-44b3-b354-4fd16d92c74e \xe2\x94\x82 CitableImage \nRun Code Online (Sandbox Code Playgroud)\n鉴于名称“CitableImage”,检索 uuid 的最佳方法是什么?
\n我在 DataFrame 上进行了大型模拟df,我试图将模拟结果并行化并将模拟结果保存在名为 的 DataFrame 中simulation_results。
并行化循环工作得很好。问题是,如果我要将结果存储在数组中,我会将其声明为SharedArray循环之前。我不知道如何声明simulation_results为“共享数据帧”,它对所有处理器来说都可用并且可以修改。
代码片段如下:
addprocs(length(Sys.cpu_info()))
@everywhere begin
using <required packages>
df = CSV.read("/path/data.csv", DataFrame)
simulation_results = similar(df, 0) #I need to declare this as shared and modifiable by all processors
nsims = 100000
end
@sync @distributed for sim in 1:nsims
nsim_result = similar(df, 0)
<the code which for one simulation stores the results in nsim_result >
append!(simulation_results, nsim_result)
end
Run Code Online (Sandbox Code Playgroud)
问题在于,由于simulation_results未声明为由处理器共享和可修改,因此在循环运行后,它基本上会生成一个空的 DataFrame,如@everywhere simulation_results = similar(df, 0) …
我有一个 df ,其中包含nothing,NaN和missing。删除包含missing我可以使用的行dropmissing。有什么方法可以处理NaN和nothing吗?
示例 df:
? Row ? x ? y ?
? ? Union…? ? Char ?
????????????????????????
? 1 ? 1.0 ? 'a' ?
? 2 ? missing ? 'b' ?
? 3 ? 3.0 ? 'c' ?
? 4 ? ? 'd' ?
? 5 ? 5.0 ? 'e' ?
? 6 ? NaN ? 'f' ?
Run Code Online (Sandbox Code Playgroud)
预期输出:
? Row ? x ? …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的 df,我想从 df 获取字典。
\ndf = DataFrame(id=[1, 2, 3, 4], value=["Rajesh", "John", "Jacob", "sundar"], other=[0.43, 0.42,0.54, 0.63])\n\n\xe2\x94\x82 Row \xe2\x94\x82 id \xe2\x94\x82 value \xe2\x94\x82 other \xe2\x94\x82\n\xe2\x94\x82 \xe2\x94\x82 Int64 \xe2\x94\x82 String \xe2\x94\x82 Float64 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 1 \xe2\x94\x82 1 \xe2\x94\x82 Rajesh \xe2\x94\x82 0.43 \xe2\x94\x82\n\xe2\x94\x82 2 \xe2\x94\x82 2 \xe2\x94\x82 John \xe2\x94\x82 0.42 \xe2\x94\x82\n\xe2\x94\x82 3 \xe2\x94\x82 3 \xe2\x94\x82 Jacob \xe2\x94\x82 0.54 \xe2\x94\x82\n\xe2\x94\x82 4 \xe2\x94\x82 4 \xe2\x94\x82 sundar \xe2\x94\x82 0.63 \xe2\x94\x82\nRun Code Online (Sandbox Code Playgroud)\n预期输出:
\n{1: 'Rajesh', 2: 'John', 3: 'Jacob', 4: 'sundar'}\nRun Code Online (Sandbox Code Playgroud)\n我知道如何在熊猫中做到这一点,
\ndf.set_index("id")["value"].to_dict()\n …Run Code Online (Sandbox Code Playgroud) 我想用它PyJulia来加速代码的某些部分
import numpy as np
import julia
import pandas as pd
import random
from julia import Base
from julia import Main
from julia import DataFrames
n = 100000
randomlist = []
for i in range(0,n):
num = random.randint(1,100)
randomlist.append(num)
data = {
'Score': list(randomlist),
'ScoreBin': list(np.zeros(n))
}
df = pd.DataFrame(data, columns = ['Score', 'ScoreBin'])
Main.dfj = df
Main.eval("""
for i = 1:10
#println(i)
if dfj.Score[i] >= 10
println(dfj.Score[i])
end
end
"""
)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:
JuliaError: Exception 'TypeError: non-boolean (PyObject) …Run Code Online (Sandbox Code Playgroud)