小编sha*_*gtq的帖子

d3.js:访问数据嵌套2级

数据结构:

var data = [ 
   {name: "male",
   values: [
     { count: 12345,
       date: Date 2015-xxx,
       name: "male" },
     {...}
   ]
  },
  {name: "female",
   values: [
     { count: 6789,
       date: Date 2015-xxx,
       name: "female" },
     {...}
   ]
  }
]
Run Code Online (Sandbox Code Playgroud)

我想要访问的值是data [a] .values [b] .count

这些值用于为我的情节绘制圆圈

圆图的代码:

focus.selectAll(".dot")
    .data(data)
    .enter().append("circle")
    .attr("class", "dot")
    .attr("cx", function(d,i) { return x(d.values[i].date); })
    .attr("cy", function(d,i) { return y(d.values[i].count); })
    .attr("r", 4)
    .style("fill", function(d,i) { return color(d.values[i].name); })
Run Code Online (Sandbox Code Playgroud)

问题是i = 1因为它在对象中的位置.

我想要做的是遍历所有objects下面values.我怎样才能做到这一点? …

javascript json d3.js

5
推荐指数
2
解决办法
1485
查看次数

熊猫将年份整数列转换为日期时间

我在将列(datatype:int64)转换为使用熊猫的日期时间时遇到问题。

原始数据:

Year
2015
2014
...
2010
Run Code Online (Sandbox Code Playgroud)

期望的结果:

Year
2015-01-01
2014-01-01
...
2010-01-01
Run Code Online (Sandbox Code Playgroud)

我目前的结果:

Year
1970-01-01 00:00:00.000002015
1970-01-01 00:00:00.000002014
...
1970-01-01 00:00:00.000002010
Run Code Online (Sandbox Code Playgroud)

我试过了:

data.Year = pd.to_datetime(data.Year)
data.Year = pd.to_datetime(data.Year, format='%Y-%m-%d')
Run Code Online (Sandbox Code Playgroud)

谢谢

datetime python-3.x pandas

3
推荐指数
1
解决办法
4052
查看次数

将单引号和双引号转换为int

使用python 将'"1"'or或"'1'"(string)转换为1(int)的最佳解决方案是什么?

int()方法将返回此值错误消息

ValueError: invalid literal for int() with base 10: "'1'"
Run Code Online (Sandbox Code Playgroud)

如果你尝试int('"1"')int("'1'")

我使用的解决方案如下:

tmp = '"1"'
tmp = tmp.replace('"', "")
tmp

# output: 1
Run Code Online (Sandbox Code Playgroud)

这种"解决方案"的缺陷是内部引用(单/双)很重要.

python types numpy python-3.x pandas

0
推荐指数
1
解决办法
43
查看次数

标签 统计

pandas ×2

python-3.x ×2

d3.js ×1

datetime ×1

javascript ×1

json ×1

numpy ×1

python ×1

types ×1