小编ytu*_*ytu的帖子

用不同的颜色绘制点标记和线条,但与Seaborn的样式相同

给定以下数据框:

import pandas as pd
df = pd.DataFrame({
    "n_index": list(range(5)) * 2,
    "logic": [True] * 5 + [False] * 5,
    "value": list(range(5)) + list(range(5, 10))
})
Run Code Online (Sandbox Code Playgroud)

我想只用颜色来区分logic线条图中的颜色,并在values 上标记点。具体来说,这是我想要的输出(由R ggplot2绘制):

ggplot(aes(x = n_index, y = value, color = logic), data = df) + geom_line() + geom_point()
Run Code Online (Sandbox Code Playgroud)

期望的输出

我尝试使用做相同的事情seaborn.lineplot,并且指定了markers=True但没有标记:

import seaborn as sns
sns.set()
sns.lineplot(x="n_index", y="value", hue="logic", markers=True, data=df)
Run Code Online (Sandbox Code Playgroud)

sns无标记

然后,我尝试添加style="logic"代码,现在标记出现了:

sns.lineplot(x="n_index", y="value", hue="logic", style="logic", markers=True, data=df)
Run Code Online (Sandbox Code Playgroud)

带有标记的sns 1

我也尝试将标记设置为相同样式:

sns.lineplot(x="n_index", …
Run Code Online (Sandbox Code Playgroud)

python data-visualization aesthetics seaborn

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

在PostgreSQL中创建一个自动增加的主键列

我正在按照CREATE TABLE 中的示例进行操作:

CREATE TABLE distributors (
     did    integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
     name   varchar(40) NOT NULL CHECK (name <> '')
);
Run Code Online (Sandbox Code Playgroud)

但是,它给了我ERROR: syntax error at or near "GENERATED"。为什么会这样,我该如何解决?

  • \! psql -V返回psql (PostgreSQL) 10.5 (Ubuntu 10.5-1.pgdg14.04+1)
  • SELECT version();在 x86_64-pc-linux-gnu (Ubuntu 9.4.19-1.pgdg14.04+1) 上返回PostgreSQL 9.4.19,由 gcc (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4 编译,64 位

编辑:

感谢 @muistooshort,我检查了 9.4 文档。所以我执行:

CREATE TABLE distributors (
     did    integer PRIMARY KEY DEFAULT nextval('serial'),
     name   varchar(40) …
Run Code Online (Sandbox Code Playgroud)

postgresql create-table

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

尝试在DataFrame.pivot中具有多索引时发生ValueError

我读过熊猫:如何使用多索引运行数据透视?但这不能解决我的问题。

给定以下数据框:

import pandas as pd
df = pd.DataFrame({
    "date": ["20180920"] * 6,
    "id": ["A123456789"] * 6,
    "test": ["a", "b", "c", "d", "e", "f"],
    "result": [70, 90, 110, "(-)", "(+)", 0.3],
    "ref": ["< 90", "70 - 100", "100 - 120", "(-)", "(-)", "< 1"]
})
Run Code Online (Sandbox Code Playgroud)

我想散布该test列,使用中的值result,然后忽略ref。换句话说,所需的输出如下:

       date          id      a   b    c    d    e    f
0  20180920  A123456789     70  90  110  (-)  (+)  0.3
Run Code Online (Sandbox Code Playgroud)

因此,我尝试了一下df.pivot(index=["date", "id"], columns="test", values="result"),但由于ValueError失败:传递的值的长度为6,索引隐含2 …

python pivot dataframe pandas

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

在Python中使用字符串调用内置函数

我参考了以下主题,我不认为这篇文章与其中任何主题重复:

  1. 使用模块的名称(字符串)调用模块的函数
  2. Python 从字符串调用函数
  3. 在Python中使用字符串调用函数

例如,假设我有一个字符串,data_type = "int"并且我想直接使用该字符串调用内置函数int。顺便说一句,我不能拥有data_type = int,因为data_type实际上是从 JSON 文件加载的,即data_type始终是字符串或None.

我最好的(最巧妙的)尝试是eval(data_type)("4"),但正如人们所建议的,这eval似乎不是一个好的选择,应该避免。

另一种方法是创建一个类似的字典data_type_dic = {"int": int, "float": float}并执行data_type_dic[data_type]("4")。然而,创建这本词典对我来说就像“重新发明轮子”。

由于int是内置函数,而不是模块中的方法,因此getattr似乎行不通。它也不是一个自定义函数,因此locals()[data_type]给出KeyError.

那么使用相应的字符串调用内置函数的最佳方法是什么?

python function-calls

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