小编Par*_*ark的帖子

在 python 数据框中创建嵌套列

我有 3 列,即模型(应作为索引)、无归一化的准确度、归一化后的准确度(zscore、minmax、maxabs、robust),这些需要创建为:

 ------------------------------------------------------------------------------------
|   Models  |  Accuracy without normalization    |      Accuracy with normalization  |
|           |                                    |-----------------------------------|
|           |                                    | zscore | minmax | maxabs | robust |
 ------------------------------------------------------------------------------------

Run Code Online (Sandbox Code Playgroud)
dfmod-> Models column
dfacc-> Accuracy without normalization
dfacc1-> Accuracy with normalization - zscore
dfacc2-> Accuracy with normalization - minmax
dfacc3-> Accuracy with normalization - maxabs
dfacc4-> Accuracy with normalization - robust
Run Code Online (Sandbox Code Playgroud)
dfout=pd.DataFrame({('Accuracy without Normalization'):{dfacc},
     ('Accuracy using Normalization','zscore'):{dfacc1},
     ('Accuracy using Normalization','minmax'):{dfacc2},
     ('Accuracy using Normalization','maxabs'):{dfacc3},
     ('Accuracy using Normalization','robust'):{dfacc4},
   },index=dfmod
)
Run Code Online (Sandbox Code Playgroud)

我试图做这样的事情,但我无法进一步弄清楚

测试数据: …

python dataframe

6
推荐指数
1
解决办法
1759
查看次数

当我继承 str 类时 super().__init__() 如何工作?

我试图继承str阶级是为了好玩。

我提供了两种方法,1)使用super()和2)str在构造函数中使用类,如下所示:

class Str2(str):
    def __init__(self, value):
        super().__init__()  # I did not use `value` here, but my code works!

    def ishello(self):
        if self == "Hello":
            return True
        else:
            False


s = Str2("Hello")
print(s.upper())  # a method of str class
print(s.ishello())  # a new method I defined in Str2 class 
Run Code Online (Sandbox Code Playgroud)
class Str2(str):
    def __init__(self, value):
        str.__init__(value)  # It makes sense for me

    def ishello(self):
        if self == "Hello":
            return True
        else:
            False


s = Str2("Hello") …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
1
解决办法
190
查看次数

如何将字典作为一行追加到 DataFrame 中?

您好,我想将字典附加到 DataFrame,但在这个字典中我没有任何索引值。我还需要从“KP”值中删除除“1393”之外的所有内容。这看起来像这样:

对此 df:在此处输入图像描述

我想像这样附加字典:

dict = {'KP': [1.0    1393
  Name: KP, dtype: int64],
 'Wiek': [Timedelta('176 days 12:59:43.042156102')],
 'KY1': [113.0],
 'OKO': [57.51],
 'GS': [10.59],
 'T': [654.31],
 'MP': [58.9]}
Run Code Online (Sandbox Code Playgroud)

我试试这个:

dict = {'KP': [1.0    1393
  Name: KP, dtype: int64],
 'Wiek': [Timedelta('176 days 12:59:43.042156102')],
 'KY1': [113.0],
 'OKO': [57.51],
 'GS': [10.59],
 'T': [654.31],
 'MP': [58.9]}
Run Code Online (Sandbox Code Playgroud)

但什么也没发生, df 仍然相同,我没有收到任何错误。我尝试将此字典转换为列表并将列表作为行附加到 df 但我得到了相同的结果。

好的,我试试这个:

df.append(dict,ignore_index=True)
Run Code Online (Sandbox Code Playgroud)

我得到这个输出: 在此处输入图像描述

这还不够好,我需要清晰的数据。

python pandas

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×3

dataframe ×1

pandas ×1