给定以下数据框:
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)
然后,我尝试添加style="logic"代码,现在标记出现了:
sns.lineplot(x="n_index", y="value", hue="logic", style="logic", markers=True, data=df)
Run Code Online (Sandbox Code Playgroud)
我也尝试将标记设置为相同样式:
sns.lineplot(x="n_index", …Run Code Online (Sandbox Code Playgroud) 我正在按照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) 我读过熊猫:如何使用多索引运行数据透视?但这不能解决我的问题。
给定以下数据框:
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 …
我参考了以下主题,我不认为这篇文章与其中任何主题重复:
例如,假设我有一个字符串,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 ×3
aesthetics ×1
create-table ×1
dataframe ×1
pandas ×1
pivot ×1
postgresql ×1
seaborn ×1