使用熊猫到csv确定文件名

时间:2020-05-07 17:38:55

标签: python pandas csv

所以我有一个脚本能够找到不同数据帧的z得分。

我希望脚本根据要应用脚本的数据框的名称以特定名称保存文件(如果我将其应用于多个数据框)。

这是我的数据框:

data = pd.DataFrame({'col1': [1,2,3,4,5,6,7,8,9,10]})

这是我正在使用的代码:

import numpy as np
import pandas as pd
from statistics import mean
import csv

class stats:
    def __init__(self, df):
        self.df = df
        self.min = self.get_min()
        self.pos = self.get_pos()
        self.log = self.get_log()
        self.z = self.get_z()
        #self.mean = self.get_mean()

    def get_min(self):
        get_min = abs(np.amin(self.df))
        return get_min

    def get_pos(self):
        get_pos = self.df + self.get_min() + 1
        return get_pos

    def get_log(self):
        get_log = np.log2(self.get_pos())
        return get_log

    def get_z(self):
        z_score = []
        get_z = (self.get_log() - np.mean(self.get_log()))/np.std(self.get_log(), ddof=1)
        z_score.append(get_z)
        return get_z

    def save_z(self):
        self.get_z().to_csv("z_" + str(z_data) + ".csv")


data = pd.DataFrame({'col1': [1,2,3,4,5,6,7,8,9,10]})
data_list = list(data.columns)
z_data = stats(df=(data[data_list]))

#print(z_data.get_min())
#print(z_data.get_pos())
#print(z_data.get_log())
#print(z_data.get_z())

if __name__ == '__main__':
    z_data.save_z()

我要修复的部分是这个:

def save_z(self):
        self.get_z().to_csv("z_" + str(data) + ".csv")

我希望文件的名称为“ z_data.csv”(如果数据框名称为x,则名称应为“ z_x.csv”)。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您想要变量的名称吗?由于与python对象模型相关的各种复杂原因,您不能*这样做(*容易,或者没有捕获)。

将字符串传递给您的班级更加清晰和可靠:

import numpy as np
import pandas as pd
from statistics import mean
import csv

class stats:
    def __init__(self, df, label):
        self.df = df
        self.label = label
        self.min = self.get_min()
        self.pos = self.get_pos()
        self.log = self.get_log()
        self.z = self.get_z()
        #self.mean = self.get_mean()

    def get_min(self):
        get_min = abs(np.amin(self.df))
        return get_min

    def get_pos(self):
        get_pos = self.df + self.get_min() + 1
        return get_pos

    def get_log(self):
        get_log = np.log2(self.get_pos())
        return get_log

    def get_z(self):
        z_score = []
        get_z = (self.get_log() - np.mean(self.get_log()))/np.std(self.get_log(), ddof=1)
        z_score.append(get_z)
        return get_z

    def save_z(self):
        self.get_z().to_csv("z_" + self.label + ".csv")


data = pd.DataFrame({'col1': [1,2,3,4,5,6,7,8,9,10]})
data_list = list(data.columns)
z_data = stats(df=data[data_list], label='z')

#print(z_data.get_min())
#print(z_data.get_pos())
#print(z_data.get_log())
#print(z_data.get_z())

if __name__ == '__main__':
    z_data.save_z()