小编bil*_*999的帖子

Python Selenium陈旧元素修复

我在python中使用Selenium时收到以下错误:

selenium.common.exceptions.StaleElementReferenceException: Message: u'stale element reference: element is not attached to the page document\n
Run Code Online (Sandbox Code Playgroud)

有趣的是,错误在for循环中的不同时间弹出.有时它通过例如.4次迭代和其他时间,例如.7.

正在运行的一些相关代码是:

for i in range(0, 22):
    u = driver.find_elements_by_id("data")
    text = u[0].get_attribute("innerHTML")
    driver.find_elements_by_class_name("aclassname")[0].click()
Run Code Online (Sandbox Code Playgroud)

这个错误意味着什么,我可以尝试解决这个问题?

html python dom selenium-webdriver

37
推荐指数
8
解决办法
5万
查看次数

pandas - 如何访问pandas中的单元格,相当于R中的df [3,4]

如果我有一个pandas DataFrame对象,我如何只是访问一个单元格?在R中,假设我的data.frame被称为df,我可以访问第3行和第4列

df[3,4]
Run Code Online (Sandbox Code Playgroud)

python中的等价物是什么?

indexing dataframe pandas

21
推荐指数
2
解决办法
4万
查看次数

如何总结数据框的对角线

假设我有这个数据框:

     1   2   3   4      
100  8   12  5   14 
99   1   6   4   3   
98   2   5   4   11  
97   5   3   7   2   
Run Code Online (Sandbox Code Playgroud)

在上面的数据框中,这些值表示观察的观察次数(100, 1), (99, 1)等.

在我的上下文中,对角线具有相同的含义:

     1   2   3   4
100  A   B   C   D 
99   B   C   D   E  
98   C   D   E   F 
97   D   E   F   G
Run Code Online (Sandbox Code Playgroud)

如何在第一个数据框中对角线(即,相似字母的数量之和)求和?

这会产生:

group  sum
A      8
B      13
C      13
D      28
E      10
F      18
G      2
Run Code Online (Sandbox Code Playgroud)

例如,D5+5+4+14

r sum dataframe diagonal

10
推荐指数
4
解决办法
1839
查看次数

无法复制此 ggplot2 图

我无法从ggrough库中复制示例( https://xvrdm.github.io/ggroough/articles/Customize%20chart.html )。特别是,我试图复制以下情节(减去字体方面):

在此处输入图片说明

代码来自上面“幼儿园”标题下的相同链接。

我正在使用以下代码:

library(hrbrthemes)
library(tidyverse)
library(gcookbook)
library(ggplot2)
library(ggrough)
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + 
    geom_area(alpha=0.8) +
    scale_fill_ipsum() +
    scale_x_continuous(expand=c(0,0)) +
    scale_y_comma() -> p 

options <- list(GeomArea=list(fill_style="hachure", 
                              angle_noise=0.5,
                              gap_noise=0.2,
                              gap=1.5,
                              fill_weight=1))
get_rough_chart(p, options)
Run Code Online (Sandbox Code Playgroud)

但是,我无法复制上述内容。这是我得到的:

在此处输入图片说明

同样,我不担心字体,但确实想让阴影 geom_area 工作。它目前根本不渲染。作为参考,这里是p对象产生的结果(即,经过ggrough处理之前的图):

在此处输入图片说明

另请注意,我能够复制“蓝图”示例,该示例使用geom_col. 所以看来事情错用ggrough处理geom_area,但不能肯定。

r ggplot2 ggrough

7
推荐指数
2
解决办法
235
查看次数

Python - pandas - 将系列追加到空白DataFrame中

说我在python中有两个pandas系列:

import pandas as pd
h = pd.Series(['g',4,2,1,1])
g = pd.Series([1,6,5,4,"abc"])
Run Code Online (Sandbox Code Playgroud)

我可以用h创建一个DataFrame,然后将g附加到它:

df = pd.DataFrame([h])
df1 = df.append(g, ignore_index=True)
Run Code Online (Sandbox Code Playgroud)

我明白了:

>>> df1
   0  1  2  3    4
0  g  4  2  1    1
1  1  6  5  4  abc
Run Code Online (Sandbox Code Playgroud)

但现在假设我有一个空的DataFrame,我尝试将h追加到它:

df2 = pd.DataFrame([])
df3 = df2.append(h, ignore_index=True)
Run Code Online (Sandbox Code Playgroud)

这不起作用.我认为问题出现在倒数第二行的代码中.我需要以某种方式定义空白DataFrame以具有适当数量的列.

顺便说一句,我试图这样做的原因是我使用requests + BeautifulSoup从互联网上抓文本,我正在处理它并尝试一次将它写入一行DataFrame.

python matrix dataframe pandas

6
推荐指数
1
解决办法
2万
查看次数

使用gganimate和view_follow和geom_tile时如何摆脱由coord_flip引起的轴闪烁?

假设我们有这个带有缩放比例的条形图竞赛x-axis。从这个考虑的代码完全回答@乔恩春季和添加(有生线之前)非常最后一行:

library(tidyverse)
library(gganimate)
library(gapminder)
theme_set(theme_classic())

gap <- gapminder %>%
    filter(continent == "Asia") %>%
    group_by(year) %>%
    # The * 1 makes it possible to have non-integer ranks while sliding
    mutate(rank = min_rank(-gdpPercap) * 1) %>%
    ungroup()

p <- ggplot(gap, aes(rank, group = country, 
                     fill = as.factor(country), color = as.factor(country))) +
    geom_tile(aes(y = gdpPercap/2,
                  height = gdpPercap,
                  width = 0.9), alpha = 0.8, color = NA) +

    # text in x-axis (requires clip = "off" in …
Run Code Online (Sandbox Code Playgroud)

r flicker ggplot2 gganimate

6
推荐指数
1
解决办法
246
查看次数

如何着色形状

是否可以使用ggroughhttps://xvrdm.github.io/ggroough/index.html)来遮蔽由geom_sf(首选)或可能创建的形状geom_polygon?请参阅此问题以了解先前的问题,该问题给出了我想到的情节的外观以及 Z.Lin 的随附答案,该答案修改了包以使其与当前版本兼容ggplot2无法复制此 ggplot2 情节

这是使用创建的地图的 MWE geom_sf,我想使用以下方法对(每个单独的县)进行着色ggrough

library(tidyverse)
library(magrittr)
library(ggplot2)
library(ggrough)
library(RColorBrewer)
library(tidycensus)
library(viridis)
#install.packages("devtools") # if you have not installed "devtools" package
#devtools::install_github("xvrdm/ggrough")
library(hrbrthemes)

#get nevada shapefile
counties <- get_acs(
    geography = "county", year = 2018, geometry = TRUE,
    variables = "B19013_001", keep_geo_vars=TRUE
) %>% filter(STATEFP=="32")
counties$GEOID <- as.integer(counties$GEOID)
#############

a <- ggplot() +
    geom_sf(data = counties, aes(fill = estimate)) + 
    scale_fill_viridis(discrete=FALSE, name="", …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggrough

6
推荐指数
1
解决办法
243
查看次数

情节不适用于循环

我需要制作一堆单独的图并希望在for循环中完成此操作.我正在使用ggplot2.我只是使用facet选项,如果它可以将每个图形保存在一个单独的文件中,我认为它不能做.

由于图表未保存到文件中,因此会发生一些事情.但是生成了文件,但这些文件都是空的.这是我的代码的样子:

for(i in 1:15) {    
pdf(paste("path/plot", i, ".pdf", sep=""), width=4, height=4)

abc <- ggplot(data[data[,3]==i,], 
              aes(variable, value, group=Name, color=Name)) + 
  geom_point(alpha=.6, size=3)+geom_line() + 
  theme(legend.position="none", axis.text.x = element_text(angle = -330)) + 
  geom_text(aes(label=Name),hjust=0, vjust=0, size=2.5) + 
  ggtitle("Title")

abc

dev.off()
}
Run Code Online (Sandbox Code Playgroud)

如何将图表保存到这些文件中?

请注意,如果我有一个数值并且我在for循环中运行代码,那么一切正常.

pdf plot for-loop r ggplot2

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

使用 gganimate 时 ggplot geom_polygon 的奇怪行为

说我有这些数据。一个用于对州的县进行着色,另一个用于绘制点。

library(tidyverse)
library(gganimate)
devtools::install_github("UrbanInstitute/urbnmapr")
library(urbanmapr)

#1. counties dataset for the shading of the background
data(counties)
#keep only Texas counties
counties <- filter(counties, state_fips==48)

#2. dots dataset for dots over time
dots <- data.frame(group = c(rep(1,3), rep(2,3)), 
                    lat = c(rep(32, 3), rep(33, 3)),
                    long = c(rep(-100, 3), rep(-99,3)), 
                    year = c(1:3, 1:3))
Run Code Online (Sandbox Code Playgroud)

然后我绘制它们(首先不使用gganimate):

ggplot() +
    geom_polygon(data = counties, aes(long, lat, group = county_fips, fill = as.numeric(county_fips))) +
    scale_fill_gradient(low = "navy", high = "lightskyblue3")   +   
    geom_point(data = dots, aes(long, lat, …
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2 gganimate

5
推荐指数
1
解决办法
605
查看次数

使用 Docker 提取 OSRM 数据时出现问题

我正在尝试提取 OSRM 数据但遇到麻烦。在上一个问题(尝试提取 osrm 数据时 lua error)中,我遇到了错误。所以除了 Docker 之外,我不会尝试做同样的事情。我正在使用带有硅芯片的较新 Mac。我首先安装了 Docker(https://docs.docker.com/desktop/install/mac-install/;然后在终端中docker pull osrm/osrm-backend)。我从https://download.geofabrik.de/north-america.html下载北美数据。我为实例分配了 50GB 内存以及几个核心。如果在终端中运行,docker run -t -v /Users/[name]/osrm-backend/docker:/data osrm/osrm-backend osrm-extract -p /opt/car.lua /data/north-america-latest.osm.pbf我会得到:

WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
[info] Parsed 0 location-dependent features with 0 GeoJSON polygons
[info] Using script /opt/car.lua
[info] Input file: north-america-latest.osm.pbf
[info] Profile: car.lua
[info] Threads: 5
[info] Parsing in …
Run Code Online (Sandbox Code Playgroud)

lua openstreetmap docker osrm osmium

5
推荐指数
1
解决办法
818
查看次数