gpt4 book ai didi

python - 如何使用 Plotly 绘制循环的局部变量

转载 作者:太空宇宙 更新时间:2023-11-03 19:53:43 24 4
gpt4 key购买 nike

我正在处理 .csv 文件。我编写脚本根据“;”拆分 y 列 的子列并且只打印 a 的值。该代码正确打印所需的值。我想使用plotly绘制存储在var中的值(即=23,21,25,12,18,91,21)。我附上了示例数据和代码。

示例数据:

   x    y   z
lifelock a=23;b=25;c=12 USD
lifelock a=21;b=55;c=23 USD
lifelock a=25;b=2.c=0 USD
mycityfaces a=12;b=7;c=21 USD
flypaper a=18;b=25;c=9 USD
flypaper a=91;b=34;c=21 USD
gauto a=21;b=77;c=81 USD

这是代码:

import pandas as pd
c=pd.read_csv("data-2.csv")
st="a"
for line in range(c.shape[0]):
cells = df["y"][line].split(";")
for x in cells:
if x.startswith("a"):
var=x[2:]
print(var)

Output:
23
21
25
12
18
91
21
#plotly code
#import plotly.express as px
#fig = px.bar(x='x',y='**var**')
#fig.show()

最佳答案

我仔细研究了您的问题,请注意您的数据集,以下行有错误:lifelock a=25;b=2.c=0 USD(出现了一个点,而不是分号)。

这是我猜您正在尝试的一个版本:

import pandas as pd
# Your conversion from c to df is not necessary
# since read_csv will return you a DataFrame already
df=pd.read_csv("data-2.csv")

# We only take the column on which we want to work.
column = df['y']
# We initialize our plot data
x = []
y = []
for line in range(len(column)):
cell = column[line]
# the tuple enhance readability a bit with your data
(a, b, c) = cell.split(";")
content = a.split('=')
# If correctly formed, add the data to your plot
if len(content) == 2:
x.append(line)
y.append(content[1])


#plotly code
import plotly.graph_objects as go
# I used a quick example from the documentation:
# https://plot.ly/python/bar-charts/#bar-chart-with-hover-text
fig = go.Figure(data=[go.Bar(x=x, y=y)])
fig.show()

关于python - 如何使用 Plotly 绘制循环的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59670011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com