【问题标题】:How to Add a Row of Values from One DF to Another如何将一行值从一个 DF 添加到另一个
【发布时间】:2020-05-09 00:05:37
【问题描述】:

我正在尝试遍历表,并根据其列值将一行复制/附加到另一个表。因此,如果第 3 列中的值为 -1,则将整行复制到另一个表 (t_neg1)。


cluster_table = clusters
table                        = [cluster_table, Label]
cluster_table                = pd.concat(table,  axis = 1, sort = False)
col                          = list(["X", "Y", "Cluster ID"])

# This part below here is causing me issues.
t_neg1 = pd.DataFrame()
t_0    = pd.DataFrame()

# I'm sures there's a better way to handle this but I'll look into it later
i = 0
for i in range(len(cluster_table)):
    if cluster_table['Cluster ID'].loc == -1:
        t_neg1.copy(cluster_table.loc[i])
    if cluster_table['Cluster ID'].loc == 0:
        t_0.copy(cluster_table.loc[i])


        i += 1

print(t_neg1)

谁能看看我的代码并告诉我我做错了什么。

【问题讨论】:

  • 一栏是什么?你是如何操纵这些数据的?始终共享整个错误消息以及minimal reproducible example。顺便说一下,将变量命名为 minmax 是个坏主意。

标签: python pandas loops iteration


【解决方案1】:

您使用 minmax 作为变量名,但它们是内置函数。 尝试重命名您的变量。

https://www.geeksforgeeks.org/max-min-python/

【讨论】:

    【解决方案2】:

    在Python中没有内置数据类型column,所以你应该给我们一个例子。

    正如错误消息所暗示的,您正在使用 Python 内置函数 wrong

    minmax 是 Python 函数 - 您不能添加或减去它们。

    虽然可以重新声明 minmax,但请不要这样做 - 因为可能会发生此类错误。

    如果您找不到更好的名称,请使用min_max_ - 附加下划线以防止隐藏内置函数是 Python 的常见做法。

    【讨论】:

      【解决方案3】:

      值 = (i - min) / (max - min)

      这里的 min 和 max 是“builtin_function_or_method”而不是值(int/float/double)。因此操作“-”有错误类型的错误操作数。

      根据您手头的需要,首先将值分配给 min & max,然后您可以按原样使用。 到

      【讨论】:

        【解决方案4】:

        你的代码不完整,但看起来你正在寻找这个:

        for n, i in enumerate(fcma):
            value = (i - min) / (max - min)
            fcma[n] =value
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-08
          • 2021-04-03
          • 1970-01-01
          • 2014-01-09
          • 1970-01-01
          • 2022-12-03
          相关资源
          最近更新 更多