- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 CSV 文件中,我有三列 z、x、y,其中“z”列用于对 x 和 y 列进行分组,并将绘制 w.r.t“z”。下面是 z,x,y 的表格。
z x y
23 1,75181E-07 6,949512
23 8,88901E-07 6,963877
23 1,61279E-05 7,293052
23 5,35262E-05 8,135064
23 8,56942E-05 8,903738
23 0,000114883 9,579907
23 0,01068653 211,0798
23 0,01070811 211,3568
23 0,0107263 211,5871
23 0,01074606 211,8401
23 0,01076813 212,1311
23 0,01078525 212,3436
40 1,75181E-07 6,949513217
40 8,889E-07 6,96388319
40 1,61277E-05 7,293169621
40 5,35248E-05 8,135499439
40 0,00029527 13,63721607
40 0,000319049 14,1825142
40 0,000340228 14,69608917
40 0,014110191 252,3893548
40 0,014132366 252,5804547
40 0,014155023 252,8030254
40 0,014180293 253,0374241
40 0,014202693 253,1983821
40 0,014226167 253,4140887
40 0,014251631 253,6566835
40 0,014272699 253,8120535
现在我需要用一个等式将“x”和“y”列替换为新值,例如“x1”和“y1”:x1 = ln(1 + x) 和 y1 = y*(1+x)和w.r.t.相同的 'z' 列我应该绘制 x1 和 y1。
我已经尝试了下面的代码,我能够看到我的新值,但无法使用新值进行绘图。
import csv
import os
import tkinter as tk
import sys
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
import math
from tkinter import ttk
import numpy as np
def readCSV(self):
x=[] # Initializing empty lists to store the 3 columns in csv
y=[]
z=[]
global df
self.filename = filedialog.askopenfilename()
df = pd.read_csv(self.filename, error_bad_lines=False) #Reading CSV file using pandas
read = csv.reader(df, delimiter = ",")
fig = plt.figure()
ax= fig.add_subplot(111)
df.set_index('x', inplace=True) #Setting index
line = df.groupby('z')['y'].plot(legend=True,ax=ax) #grouping and plotting
cursor = datacursor(line)
gdf= df[df['z'] == 23]
x=np.asarray(gdf.index.values)
y=np.asarray(gdf['y'].values)
x1 = np.log(1+x)
y1 = y * (1 + x)
df.set_index('x1', inplace=True) #Setting new index
line = df.groupby('z')['y1'].plot(legend=True,ax=ax) #grouping and plotting for new values
cursor = datacursor(line)
gdf= df[df['z'] == 23]
x1=np.asarray(gdf.index.values)
print ("x1:",x1)
y1=np.asarray(gdf['y1'].values)
print ("y1:",y1)
ax1 = ax.twinx()
ax.grid(True)
ax.set_ylim(0,None)
ax.set_xlim(0,None)
align_yaxis(ax, y.max(), ax1, 1)
plt.show()
我在这一行“df.set_index('x1', inplace=True)”中收到错误作为 keyerror : x1
提前致谢
最佳答案
您必须将 x1
和 'y1' 分配给数据帧才能将它们中的任何一个分配给 index
:
x=np.asarray(df.index.values)
y=np.asarray(df['y'].values)
df['x1'] = np.log(1+x)
df['y1'] = y * (1+x)
关于python - Pandas : Replace old column values and plot new column values with respect to equations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49977544/
pandas.crosstab 和 Pandas 数据透视表似乎都提供了完全相同的功能。有什么不同吗? 最佳答案 pivot_table没有 normalize争论,不幸的是。 在 crosstab
我能找到的最接近的答案似乎太复杂:How I can create an interval column in pandas? 如果我有一个如下所示的 pandas 数据框: +-------+ |
这是我用来将某一行的一列值移动到同一行的另一列的当前代码: #Move 2014/15 column ValB to column ValA df.loc[(df.Survey_year == 201
我有一个以下格式的 Pandas 数据框: df = pd.DataFrame({'a' : [0,1,2,3,4,5,6], 'b' : [-0.5, 0.0, 1.0, 1.2, 1.4,
所以我有这两个数据框,我想得到一个新的数据框,它由两个数据框的行的克罗内克积组成。正确的做法是什么? 举个例子:数据框1 c1 c2 0 10 100 1 11 110 2 12
TL;DR:在 pandas 中,如何绘制条形图以使其 x 轴刻度标签看起来像折线图? 我制作了一个间隔均匀的时间序列(每天一个项目),并且可以像这样很好地绘制它: intensity[350:450
我有以下两个时间列,“Time1”和“Time2”。我必须计算 Pandas 中的“差异”列,即 (Time2-Time1): Time1 Time2
从这个 df 去的正确方法是什么: >>> df=pd.DataFrame({'a':['jeff','bob','jill'], 'b':['bob','jeff','mike']}) >>> df
我想按周从 Pandas 框架中的列中累积计算唯一值。例如,假设我有这样的数据: df = pd.DataFrame({'user_id':[1,1,1,2,2,2],'week':[1,1,2,1,
数据透视表的表示形式看起来不像我在寻找的东西,更具体地说,结果行的顺序。 我不知道如何以正确的方式进行更改。 df示例: test_df = pd.DataFrame({'name':['name_1
我有一个数据框,如下所示。 Category Actual Predicted 1 1 1 1 0
我有一个 df,如下所示。 df: ID open_date limit 1 2020-06-03 100 1 2020-06-23 500
我有一个 df ,其中包含与唯一值关联的各种字符串。对于这些唯一值,我想删除不等于单独列表的行,最后一行除外。 下面使用 Label 中的各种字符串值与 Item 相关联.所以对于每个唯一的 Item
考虑以下具有相同名称的列的数据框(显然,这确实发生了,目前我有一个像这样的数据集!:() >>> df = pd.DataFrame({"a":range(10,15),"b":range(5,10)
我在 Pandas 中有一个 DF,它看起来像: Letters Numbers A 1 A 3 A 2 A 1 B 1 B 2
如何减去两列之间的时间并将其转换为分钟 Date Time Ordered Time Delivered 0 1/11/19 9:25:00 am 10:58:00 am
我试图理解 pandas 中的下/上百分位数计算,但有点困惑。这是它的示例代码和输出。 test = pd.Series([7, 15, 36, 39, 40, 41]) test.describe(
我有一个多索引数据框,如下所示: TQ bought HT Detailed Instru
我需要从包含值“低”,“中”或“高”的数据框列创建直方图。当我尝试执行通常的df.column.hist()时,出现以下错误。 ex3.Severity.value_counts() Out[85]:
我试图根据另一列的长度对一列进行子串,但结果集是 NaN .我究竟做错了什么? import pandas as pd df = pd.DataFrame([['abcdefghi','xyz'],
我是一名优秀的程序员,十分优秀!