简体   繁体   English

如何将一行值从一个 DF 添加到另一个

[英]How to Add a Row of Values from One DF to Another

I'm trying to iterate through the table, and copy/append a row to another table based on its' column value.我正在尝试遍历表,并根据其列值将一行复制/附加到另一个表。 So if the value in the 3rd column is -1, the entire row is copied to another table (t_neg1).因此,如果第 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)

Can someone look at my code and tell me what I'm doing wrong.有人可以看看我的代码并告诉我我做错了什么。

Your code is not complete but still it looks like you are looking for this:您的代码不完整,但看起来您正在寻找这个:

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

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

Here min & max are 'builtin_function_or_method' not values(int/float/double).这里的最小值和最大值是“builtin_function_or_method”而不是值(int/float/double)。 Hence operation '-' has wrong operands of wrong types.因此操作“-”有错误类型的错误操作数。

According to your need at hand, first assign values to min & max, then you can use as is.根据您手头的需要,首先为最小值和最大值分配值,然后您可以按原样使用。 to

In Python there is no builtin data type column , so you should give us an example.在 Python 中没有内置数据类型column ,所以你应该给我们一个例子。

As the error message suggests, you are are doing something wrong with Python builtins.正如错误消息所暗示的那样,您在使用 Python 内置函数时wrong了什么。

min and max are Python functions - you cannot add or subtract them. minmax是 Python 函数——你不能对它们进行加减。

While it is possible to redeclare min and max , please do not do this - as exactly these kind of errors can happen.虽然可以重新声明minmax ,但请不要这样做 - 因为正是这些类型的错误可能会发生。

If you cannot find a better name, please use min_ and max_ - appending an underscore to prevent shadowing builtins is common Python practice.如果找不到更好的名称,请使用min_max_ - 添加下划线以防止隐藏内置函数是常见的 Python 做法。

You're using min and max as variable names but they are Built-in functions.您使用minmax作为变量名,但它们是内置函数。 Try to rename your's variables.尝试重命名您的变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个 df 的行在另一个 df 的一行中有多少次值更高 - How many times does row from one df have values higher in a row of another df 在特定行/索引的一个数据框中搜索值并将其添加到特定行/索引的另一df并将其添加 - Search for and Add Values from One Dataframe at specific row/index to another df at specific row/index append 如果 pandas 中没有重复,则从一个 df 到另一个的行值 - append row values from one df to another if no duplicates in pandas Python / Pandas:如果有匹配项,则将值从一个df添加到另一df的行末 - Python/Pandas: add value from one df to end of row in another df if there is a match 如何根据一列重新采样 df 并添加另一列的值? - How to resample df based on one column and add the values from another column? 如何将一个数据帧中的值添加到另一个数据帧中而忽略行索引 - How to add values from one dataframe into another ignoring the row indices 用另一个 df 中的值替换一个 df 中的值的最快方法 - fastest way to replace values in one df with values from another df 根据另一个df中每行的定界值创建df - Create a df from delimited values of each row in another df python - 如何将值从一个df传输到另一个 - python - how do I transfer values from one df to another 将行模式从一个 DF 应用到另一个 DF - Apply Row Pattern From One DF Onto Another DF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM