小编Meh*_*are的帖子

在单个 RDS 文件中保存多个变量

我想将变量列表传递给 saveRDS() 以保存它们的值,但它保存了它们的名称:

variables <- c("A", "B", "C")
saveRDS(variables, "file.R")
Run Code Online (Sandbox Code Playgroud)

它保存了单个向量“变量”。

我也试过:

save(variables, "file.RData")
Run Code Online (Sandbox Code Playgroud)

没有成功

r datastore save

4
推荐指数
1
解决办法
2658
查看次数

使用二级索引值将 Pandas 中的列转换为行

这里跟进我之前的问题:

import pandas as pd
d = pd.DataFrame({'value':['a', 'b'],'2019Q1':[1, 5], '2019Q2':[2, 6], '2019Q3':[3, 7]})

Run Code Online (Sandbox Code Playgroud)

显示如下:

   value    2019Q1  2019Q2  2019Q3
0  a        1       2       3
1  b        5       6       7
Run Code Online (Sandbox Code Playgroud)

我怎样才能把它变成这个形状:

Year    measure    Quarter    Value
2019    a          1          1
2019    a          2          2
2019    a          3          3
2019    b          1          5
2019    b          2          6
2019    b          3          7
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas

4
推荐指数
1
解决办法
108
查看次数

将 PNG 文件加载到 TensorFlow 中

我正在尝试加载我生成的定制 png 文件来训练我的模型。按照此处TensorFlow 指南的说明,我使用了以下代码:

import tensorflow as tf
import numpy as np
from pathlib import Path, WindowPath

train_df = pd.DataFrame(
    {'file_name': {0: WindowsPath('hypothesis/temp/81882f4e-0a94-4446-b4ac-7869cf198534.png'), 1: WindowsPath('hypothesis/temp/531162e2-2b4c-4e64-8b3f-1f285b0e1040.png')}, 'label': {0: -0.019687398020669655, 1: 0.0002379227226001479}}
)

file_path_list = [i.read_bytes() for i in train_df['file_name']]

dataset = tf.data.TFRecordDataset(filenames=file_path_list)

raw_example = next(iter(dataset))
parsed = tf.train.Example.FromString(raw_example.numpy())


Run Code Online (Sandbox Code Playgroud)

运行该raw_example...行会返回以下错误消息:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 43: invalid start byte
Run Code Online (Sandbox Code Playgroud)

我使用 matplotlib 生成了 PNG 文件。

python tensorflow tensorflow-datasets tensorflow2.0

4
推荐指数
1
解决办法
2751
查看次数

Pass a dictionary in try/except clause

I have a use case that requires passing a dictionary in a try/exception clause in Python 3.x

The error message can be accessed as a string using str() function, but I can't figure out who to get it as a dictionary.

try:
    raise RuntimeError({'a':2})
except Exception as e:
    error = e
    print(error['a'])
Run Code Online (Sandbox Code Playgroud)

e is a RuntimeError object and I can't find any method that returns the message in its original format.

python python-3.x

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

直接动态添加框到 Shiny Dashboard

我正在尝试根据矢量的内容将多个框添加到闪亮的界面。

让我们从这里开始:

library(shiny)

ui <- fluidPage(

   titlePanel("Dynamic Boxes"),

   fluidRow(
     uiOutput("boxes")
  )
)

server <- function(input, output) {

  output$boxes <- renderUI({
    interf <- ""
    for(i in 1:10){
      x = 1:100
      interf <- box(title = paste0("box ", i), 
          renderPlot(plot(x = x, y = x^i)))

    }
    interf
  })
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

它只显示最后一个框。我不知道如何将它们组合在一起,然后将其传递给客户端。

r shiny shinydashboard

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

Django使用ORM选择每组前n条记录

按照这个问题,我试图获取每个 group_by 标准的前 10 条记录,但 Django 返回此错误:

from django.db.models import F, Window
from django.db.models.functions import RowNumber

Purchases.objects.annotate(row_number=Window(
        expression=RowNumber(),
        partition_by=F('customer'),
        order_by=F('field_of_interest').desc()
        )
    ).filter(row_number=10)
Run Code Online (Sandbox Code Playgroud)
raise NotSupportedError(
django.db.utils.NotSupportedError: Window is disallowed in the filter clause.
Run Code Online (Sandbox Code Playgroud)

当我删除 .desc() 时,错误消息更改为:

ValueError: order_by must be either an Expression or a sequence of expressions.
Run Code Online (Sandbox Code Playgroud)

我正在使用 PostgreSql。这是一个错误还是我在查询中的某个地方错了?

python django postgresql

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

根据 Pandas DataFrame 中的值序列生成索引元组

这是我上一个问题的后续:根据 Pandas DataFrame 列中的值序列查找行的索引

我想得到一个索引为非常坏的元组列表,然后是第一次出现“坏”的索引:

import random

df = pd.DataFrame({
    'measure': [random.randint(0,10) for _ in range(0,20)],
})

df['status'] = df.apply(
    lambda x: 'good' if x['measure'] > 4 else 'very bad' if x['measure'] < 2  else 'bad',
    axis=1)
Run Code Online (Sandbox Code Playgroud)

这是数据框:

    measure    status
0         8      good
1         8      good
2         0  very bad
3         5      good
4         2       bad
5         3       bad
6         9      good
7         9      good
8        10      good
9         5      good
10        1  very bad
11        7      good
12 …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas

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

将函数应用于跨列的 pandas DataFrame 以创建用于排序的临时列

基于具有列值函数的 Sort pandas DataFrame

我想使用log()该方法应用一个函数,例如数据框.assign(),以创建临时列并将其用作排序标准,但是,我无法像该方法的工作方式那样传递轴参数.apply()

这是示例代码:

from numpy.random import randint

set.seed(0)
df = pd.DataFrame({'value':[randint(1,10) for i in range(0,10)], 'reading': [randint(1,10) for i in range(0,10)]})
Run Code Online (Sandbox Code Playgroud)
   value  reading
0      8        6
1      5        9
2      3        7
3      8        2
4      6        1
5      4        9
6      6        2
7      3        5
8      2        2
9      8        8
Run Code Online (Sandbox Code Playgroud)

我不能像这样使用 .assign() 方法:

df.assign(log = log(df.value/df.reading))

    raise TypeError("cannot convert the series to " "{0}".format(str(converter)))
TypeError: cannot convert …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas

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