如果我将中间步骤之一分配给变量,Python 会给出不同的结果,如下所示:
>>> -0.207 ** 0.66 - 1
-1.3536229379434348
>>> a = -0.207
>>> a ** 0.66 - 1
(-1.1703591496008927+0.30988214273656856j)
Run Code Online (Sandbox Code Playgroud)
对于这个简单的计算,如果我分配-0.207给一个临时变量a,则 的结果将a ** 0.66 - 1计算为复数。
为什么会发生这种情况?如何阻止 Python 这样做?
尝试使用plotly js 绘制日期图,但由于某种原因它显示时间和日期。
如何从该图中删除时间?谢谢!
x = ["2023-02-13", "2023-02-14"];
y = [3, 4];
var trace1 = {
type: "scatter",
mode: "lines",
name: '',
x: x,
y: y,
line: {color: '#17BECF'}
}
var layout = {
xaxis: {
type: 'date',
},
title: '',
};
var data = [trace1];
Plotly.newPlot('plotDiv', data, layout);
Run Code Online (Sandbox Code Playgroud)
我有一串球。每个球上都有一个箭头。
我想用绘图来表示上面的图形,这样我就可以旋转它,但我完全迷失了。如何将箭头定位在球上?
我开始制作前 4 个箭头,如下所示:
import plotly.graph_objs as go
import plotly
plotly.offline.init_notebook_mode()
x = [10.1219, 10.42579, 15.21396, 15.42468, 20.29639,20.46268, 25.36298, 25.49156]
y = [5.0545, 5.180104, 5.0545, 5.20337, 5.0545, 5.194271, 5.0545, 5.231627]
z = [5.2713, 5.231409, 5.2713, 5.231409, 5.2713 , 5.235852, 5.2713, 5.231627]
pairs = [(0,1), (2,3),(4,5), (6,7)]
trace1 = go.Scatter3d(
x=x,
y=y,
z=z,
mode='markers',
name='markers'
)
x_lines = list()
y_lines = list()
z_lines = list()
for p in pairs:
for i in range(2):
x_lines.append(x[p[i]])
y_lines.append(y[p[i]])
z_lines.append(z[p[i]])
x_lines.append(None)
y_lines.append(None)
z_lines.append(None)
trace2 …Run Code Online (Sandbox Code Playgroud) 我已经提到了之前的查询“所有参数应该具有相同的长度” ,但仍然没有得到我的问题的答案。
我有一个黄金价格数据集。
Date Price
31-01-1979 1840.8
28-02-1979 2011.7
30-03-1979 1940.2
30-04-1979 2013.1
. .
. .
. .
26-02-2021 128073.3
31-03-2021 123639
30-04-2021 130934.3
31-05-2021 137979.1
Run Code Online (Sandbox Code Playgroud)
我创建了 12 个月移动平均线:
df['MA12'] = df['Price'].rolling(12).mean()
Run Code Online (Sandbox Code Playgroud)

1)首先我使用以下命令:我分别得到了两个不同的价格和移动平均线图。
import plotly.express as px
fig1 = px.line(df, x="Date", y="Price", template = 'plotly_dark')
fig2 = px.line(df, x="Date", y="MA12", template = 'plotly_dark')
fig1.show()
fig2.show()
Run Code Online (Sandbox Code Playgroud)
2)现在我使用下面的命令来绘制时间序列:我想要在单个图上显示相对于日期的价格和移动平均价格趋势
import plotly.express as px
fig = px.line(df, x='Date', y=["Price","MA12"], template = 'plotly_dark')
fig.show()
Run Code Online (Sandbox Code Playgroud)
出现错误:ValueError:所有参数应具有相同的长度。参数的长度y为 2,而之前的参数 ['Date'] 的长度为 509。
我的查询:
a) …
我有一组由多面包裹的情节表达的 barplots ,每个都有一个标题。如何将每个子图的标题与其绘图窗口的左侧左对齐?

import lorem
import plotly.express as px
import numpy as np
import random
items = np.repeat([lorem.sentence() for i in range(10)], 5)
response = list(range(1,6)) * 10
n = [random.randint(0, 10) for i in range(50)]
(
px.bar(x=response, y=n, facet_col=items, facet_col_wrap=4, height=1300)
.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
.for_each_xaxis(lambda xaxis: xaxis.update(showticklabels=True))
.for_each_yaxis(lambda yaxis: yaxis.update(showticklabels=True))
.show()
)
Run Code Online (Sandbox Code Playgroud)
我尝试添加,.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1], x=0))但结果是:

我有一个数据框,列出了不同的团队(绿色、蓝色、黄色、橙色、[有数百个团队]等),并列出了他们每月的收入。我希望能够根据收入创建前 10 名团队的列表,然后将其输入到 groupby 语句中,以便我在处理各种数据帧时只查看这些团队。这些是我创建的但我遇到问题的语句:
Rev = df['Revenue'].head(10) and I have also used Rev = df.nlargest(10,['Revenue'])
grpby = df.groupby([df['team'].isin(rev), 'team'], as_index=False)['Revenue'].sum().sort_values('Revenue', ascending=False).reset_index()
Run Code Online (Sandbox Code Playgroud)
*编辑:导致此请求的其他代码:*编辑:df = pd.read_excel('c:/Test.xlsx',sheet_name =“Sheet1”,index_col = 'Date',parse_dates = True)
*编辑:df = pd.DataFrame(df)
我可以使 groupby 语句起作用,但我无法将“Rev”列表输入到限制/过滤要查看的组的 groupby 语句中。
另外,当使用 groupby 语句创建数据框时,如何添加回未分组的其他列?例如,在上面的声明中,我尝试使用“团队”和“收入”,但如果我还想添加其他列,例如(“位置”或“团队负责人”),添加更多列的语法是什么?
*通过 Excel 文件编辑示例输入:Teams Revenue Green 10 Blue 15 Red 20 Orange 5 在上面的示例中,我想使用一个语句,该语句获取前三个并保存为列表,然后将其输入到 groupby 语句中。现在看起来我还没有填写实际的数据框?*从控制台:空 DataFrame 列:[团队,收入] 索引:[]
我仍在设计我想要做的带有子图的图,但是当我们看到文档 “带有标题的多个子图”中的示例时,我们有
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(
rows=2, cols=2,
subplot_titles=("Plot 1", "Plot 2", "Plot 3", "Plot 4"))
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
row=1, col=1)
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
row=1, col=2)
fig.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800]),
row=2, col=1)
fig.add_trace(go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000]),
row=2, col=2)
fig.update_layout(height=500, width=700,
title_text="Multiple Subplots with Titles")
fig.show()
Run Code Online (Sandbox Code Playgroud)
这使
这很好,但请注意,图例只有一个位置。(迹线 0、迹线 1 等)
在我的设计中,左上角的情节有一个传说,其他三个情节有一些其他的传说。有没有办法个性化或定制子图的图例?
我试图将基因表达的变化可视化为不同时间点的分类变量(向上、向下、无变化)。
我有一个描述差异表达数据的数据框,如下所示:
data = {'gene':['Svm3G0018840','Svm5G0011050','Svm9G0059770'],
'01h': ['nc','up','down'], '04h': ['up', 'down', 'nc'],'08h':['nc','down','up']}
df=pd.DataFrame.from_dict(data)
df=df.set_index('gene')
Run Code Online (Sandbox Code Playgroud)
我可以使用此 df 使用以下代码创建并行图:
fig = px.parallel_categories(herbdf, dimensions=['01h', '04h', '08h','24h','48h'],
labels={'01h':'', '04h':'', '08h':'','24h':'','48h':''})
fig.show()
Run Code Online (Sandbox Code Playgroud)
然而,每个时间点的类别(上、下、NC)并不总是相同的顺序,这使得该图非常难以阅读。我可以在笔记本的交互式图形中更改此设置,但我只能选择将校正后的图形输出为低质量 png。我需要 svg 格式的图像,这意味着我需要使用以下行:
fig.write_image("/figs/herb_de_pp.svg")
Run Code Online (Sandbox Code Playgroud)
但是当我在代码块中添加这一行来保存图形时,我无法控制分类框最终的顺序:
我尝试添加fig.update_行来解决这个问题,例如:
fig.update_layout(xaxis={'categoryorder':'total descending'})
Run Code Online (Sandbox Code Playgroud)
但这似乎根本没有改变输出。
我可能会错过一些简单的东西 - 任何帮助将不胜感激!
我正在尝试将utf-8DataFrame 中的列转换为YYYY-MM-DD. 如何将不同的日期格式转换为一种格式YYYY-MM-DD?
s = pl.Series("date",\\["Sun Jul 8 00:34:60 2001", "12Mar2022", "12/Mar/2022"\\])\n\ndf=s.to_frame().with_columns(pl.col("date").str.strptime(pl.Date,fmt=('%d%m%Y' ), strict=False)) \n\nprint(df)\n\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\n\xe2\x94\x82 date \xe2\x94\x82\n\n\xe2\x94\x82 --- \xe2\x94\x82\n\n\xe2\x94\x82 date \xe2\x94\x82\n\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\n\xe2\x94\x82 null \xe2\x94\x82\n\n\xe2\x94\x82 null \xe2\x94\x82\n\n\xe2\x94\x82 null \xe2\x94\x82\n\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n 我需要将模型从 RGB 扩展到 RGBA。我可以处理模型上的代码重写,但我不想从头开始重新训练整个模型,而是希望从 3 个通道权重 + 零开始。
有没有简单的方法可以将手电筒的 3 个通道权重保存更改为 4 个?
如何在 Pyspark 中使用盐化技术进行倾斜聚合。
假设我们有倾斜的数据,如下所示,如何创建盐列并在聚合中使用它。
| 城市 | 状态 | 数数 |
|---|---|---|
| 拉琼 | 锡金 | 3,000 |
| 让波 | 锡金 | 50,000 |
| 甘托克 | 锡金 | 3,00,000 |
| 班加罗尔 | 卡纳塔克邦 | 2,50,00,000 |
| 孟买 | 马哈拉施特拉邦 | 2,90,00,000 |
sorted()我正在通过以下小示例研究Python 3.9 中的函数
circus = [('a', 'b', 'c', 'd', 'e'),('w', 'x', 'y', 'z', 'a'),('k', 'l', 'm', 'n', 'o'),('u', 'v', 'w', 'x', 'y'),('q', 'r', 's', 't', 'u'),('e', 'f', 'g', 'h', 'i')]
Run Code Online (Sandbox Code Playgroud)
这只是一个五元组列表,我想按第五个元素(即 e、a、o、y、u、i)排序
我知道正确的方法是
sorted(circus, key = lambda d: d[4], reverse = True)
Run Code Online (Sandbox Code Playgroud)
但我正在尝试这个
sorted(circus, lambda z: z[4], True)
Run Code Online (Sandbox Code Playgroud)
并得到错误
TypeError: sorted expected 1 argument, got 3
Run Code Online (Sandbox Code Playgroud)
我试图理解为什么它需要 1 个参数。根据文档(https://www.w3schools.com/python/ref_func_sorted.asp),另外两个是可选参数,但它们仍然应该是预期的,对吗?