我有两个数据框 df1:
d1 = {"col1" : ['A', 'B', 'C'],
"Col2": ["home", "car","banana" ]}
d2 = {"col1" : ['D', 'F','C'],
"Col2": ["garden", "boat","banana" ]}
df1 = pd.DataFrame(data=d1)
df2 = pd.DataFrame(data=d2)
new_df = pd.merge(df1, df2, on ='col1', how='outer')
new_df
Run Code Online (Sandbox Code Playgroud)
所以我想要做的是突出显示在两个数据框中找到的第三行“香蕉”。我正在使用样式文档来找到解决方案,但没有运气。我只能突出显示一行,但是当我有多行时它不起作用。请伸出援助之手
我正在尝试创建等值分布图。下面是一个有效的例子:
df = px.data.gapminder().query("year==2007")
fig = go.Figure(data=go.Choropleth(
locations=happy['iso'], # Spatial coordinates
z = happy['Happiness'].astype(float), # Data to be color-coded
colorbar_title = "Happiness Score",
))
fig.update_layout(
title_text = 'Life Expectancy in 2007'
)
fig.show()
Run Code Online (Sandbox Code Playgroud)
但是,我想创建一个下拉菜单来更改不同变量(例如,预期寿命、GDP、人口)之间的绘图值。我相信这是可能的,但还没有在网上看到任何教程。他们中的大多数只使用其他类型的条形图或散点图。
这是我到目前为止所得到的:
# Initialize figure
fig = go.Figure()
# Add Traces
fig.add_trace(go.Figure(data=go.Choropleth(
locations=df['iso_alpha'], # Spatial coordinates
z = df['lifeExp'].astype(float), # Data to be color-coded
colorbar_title = "Life Expectancy")))
fig.add_trace(go.Figure(data=go.Choropleth(
locations=df['iso_alpha'], # Spatial coordinates
z = df['gdpPercap'].astype(float), # Data to be color-coded
colorbar_title = "GDP per capita")))
Run Code Online (Sandbox Code Playgroud)
但我不确定如何从这里开始。我是否需要通过 …
以下示例的类型提示应该是什么:
import plotly.graph_objs as go
def update_scene_callback():
....
def scatter_plot(input_data, layout):
fig = go.FigureWidget(input_data)
fig.update_layout(object_layout)
scatter = fig.data[0]
scatter.on_click(update_scene_callback)
return fig
Run Code Online (Sandbox Code Playgroud)
type(fig)函数plotly.graph_objs._figurewidget.FigureWidget
的输入和输出的类型应该是什么scatter_plot?
另外,假设我有一个数据帧作为函数的输入变量。但我不希望在函数内部更改数据框。有什么方法可以为输入变量分配类型,以便我们可以确保该变量不会在函数内部更改?
Python 新手。
如何将数据从 hive 保存到 Pandas 数据框。
with pyhs2.connect(host, port=20000,authMechanism="PLAIN",user,password,
database) as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()
#Execute query
cur.execute(query)
#Return column info from query
print cur.getSchema()
#Fetch table results
for i in cur.fetch():
print i
**columnNames = [a['columnName'] for a in cur.getSchema()]
print columnNames
df1=pd.DataFrame(cur.fetch(),columnNames)**
Run Code Online (Sandbox Code Playgroud)
尝试使用列名。没用。
请。提出一些建议。
我已经阅读了几篇关于如何使用pd.to_numeric以及applymap(locale.atof)将Pandas列转换为float的帖子.
我遇到了无法解决的问题.
注意原始的Dataframe是dtype:Object
df.append(df_income_master[", Net"])
Out[76]:
Date
2016-09-30 24.73
2016-06-30 18.73
2016-03-31 17.56
2015-12-31 29.14
2015-09-30 22.67
2015-12-31 95.85
2014-12-31 84.58
2013-12-31 58.33
2012-12-31 29.63
2016-09-30 243.91
2016-06-30 230.77
2016-03-31 216.58
2015-12-31 206.23
2015-09-30 192.82
2015-12-31 741.15
2014-12-31 556.28
2013-12-31 414.51
2012-12-31 308.82
2016-10-31 2,144.78
2016-07-31 2,036.62
2016-04-30 1,916.60
2016-01-31 1,809.40
2015-10-31 1,711.97
2016-01-31 6,667.22
2015-01-31 5,373.59
2014-01-31 4,071.00
2013-01-31 3,050.20
2016-09-30 -0.06
2016-06-30 -1.88
2016-03-31
2015-12-31 -0.13
2015-09-30
2015-12-31 -0.14
2014-12-31 0.07
2013-12-31 0
2012-12-31 0
2016-09-30 -0.8
2016-06-30 …Run Code Online (Sandbox Code Playgroud) 我想知道在给定条件的情况下,在保留原始索引的同时,最有效的方法是做一个数组的argsort
import numpy as np
x = np.array([0.63, 0.5, 0.7, 0.65])
np.argsort(x)
#Corrected argsort(x) solution
Out[99]: array([1, 0, 3, 2])
Run Code Online (Sandbox Code Playgroud)
我想在x> 0.6的条件下对这个数组进行argsort.由于0.5 <0.6,因此不应包括指数1.
x = np.array([0.63, 0.5, 0.7, 0.65])
index = x.argsort()
list(filter(lambda i: x[i] > 0.6, index))
[0,3,2]
Run Code Online (Sandbox Code Playgroud)
这是低效的,因为它没有矢量化.
编辑:过滤器将消除大多数元素.理想情况下,它首先进行过滤,然后进行排序,同时保留原始索引.
无论我做什么我都会收到此错误
C:\Users\MyPc>conda update --all
Solving environment: failed
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/free/win-64/repodata.json.bz2>
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.
SSLError(MaxRetryError('HTTPSConnectionPool(host=\'repo.anaconda.com\', port=443): Max retries exceeded with url: /pkgs/free/win-64/repodata.json.bz2 (Caused by SSLError("Can\'t connect to HTTPS URL because the SSL module is not …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 python 在 Jupyter notebook 中创建一个 GUI。我已经对按钮进行了编码以使用小部件执行代码。但我遇到了两个问题:
这是我一直在尝试的:
import ipywidgets as widgets
from IPython.display import display
rs_button = widgets.Button(description="Restart Kernel!")
exec_button = widgets.Button(description="Click Me!")
display(rs_button,exec_button)
def rs_button_clicked(b):
IPython.notebook.execute_cell();
def exec_button_clicked(b):
import data_assess_v6 as data_profiler
(execution_time) = data_profiler.data_profile(path,file)
rs_button.on_click(rs_button_clicked)
exec_button.on_click(exec_button_clicked)
Run Code Online (Sandbox Code Playgroud)
谢谢
我已经使用构建了一个图表networkx,然后按照官方指南plotly中的描述将其可视化。但是,我在图表中无缘无故地得到了一个随机的“新文本”标签。该标签不会出现在网络的某个特定区域中,而是取决于我缩放的位置(因此它可能会出现在一个部分上,然后如果我缩放另一部分,它会出现在那里)。
我检查了所有标签(节点或边缘),但正如预期的那样,那里没有问题。
我什至检查了代码中任何硬编码的“新文本”部分,但一切看起来都很好。
这里可能有什么问题?
这是用于可视化它的代码:
import networkx
import plotly.graph_objs as go
# set node positions
pos = nx.nx_pydot.graphviz_layout(G2)
# Nodes information
node_x = []
node_y = []
node_labels = []
for key, value in pos.items():
x, y = value[0], value[1]
node_x.append(x)
node_y.append(y)
node_labels.append(key)
# Edges information
edge_x = []
edge_y = []
edge_labels = []
for edge in G2.edges().data():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
# …Run Code Online (Sandbox Code Playgroud) 因此,我正在尝试构建一个 Plotly sunburst 图,用于显示percentParent图中的每个元素。这适用于所有元素,除了当我只有一个中央节点/环/其他选项时(请参见下面的示例)
由于中心节点显然没有父节点,因此它似乎会出错并显示percentParent来自texttemplate字段的括号内的调用。但是,如果有 2 个(或更多)中心节点,它会自动计算每个中心节点占两个节点总和的百分比。
我的问题是:当我只有 1 个中心节点时,如何仅为中心节点隐藏此字段或使其正确显示“100%”?
示例代码:
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({'node_names': ['Center', 'Yes', 'No'],
'node_parent': ['', 'Center', 'Center'],
'node_labels': ['Center', 'Center_Yes', 'Center_No'],
'node_counts': [1000, 701, 299]})
fig = go.Figure(
data=go.Sunburst(
ids=df["node_names"],
labels=df["node_labels"],
parents=df["node_parent"],
values=df["node_counts"],
branchvalues="total",
texttemplate = ('%{label}<br>%{percentParent:.1%}'),
),
)
fig.show()
Run Code Online (Sandbox Code Playgroud)