python - Pandas : Replace old column values and plot new column values with respect to equations

标签 python pandas csv

在 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/

相关文章:

python - 如何调整 matplotlib 散点图的大小

Pandas 重新采样数据框并将日期时间索引保留为一列

java - 如何使用java 8找到两个流的交集?

python - 从一个 csv 文件中提取指定数据,然后使用 python 分配给另一个 csv 文件

java - 由于保存前/后 CSV 差异导致的错误解析(Java w/Apache Commons CSV)

python - 如何在 Django Channels 中创建服务器端计时器/倒计时?

Python 2.7 下载图像

python - 使用 matplotlib 显示稀疏箭袋箭头

python - Pandas 连接/连接操作合一以连接数据帧

python - 如何访问 python datetime.time 列中的小时数