如何将行值从一个数据框添加到另一个

时间:2020-02-25 16:06:36

标签: python pandas dataframe iteration

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

表: table image

有人可以看看我的代码,并告诉我我在做什么错吗?

代码:

import pandas as pd
from sklearn.cluster import DBSCAN
from collections import Counter
from sklearn.neighbors import NearestNeighbors
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use('TkAgg')

eps = 4.3


model  = DBSCAN(eps            = eps,
                min_samples    = 210,
                metric         = 'euclidean'
                )

data = model.fit(plotting_data)


X          = plotting_data['X']
Y          = plotting_data['Y']

clusters = data.fit_predict(plotting_data)
print(clusters)

clust_df = pd.DataFrame(plotting_data)
clusters = (clust_df[data.labels_ != -1])
Label    = data.fit_predict(clusters)

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)

0 个答案:

没有答案