我将很快将两个数据集合并3列。希望在原始数据集中没有/很少有3列组重复。我想制作一些大致说明每一行有多独特的东西。比如某种频率图(可能不起作用,因为我有一个很大的数据集(,也许是一个显示每500万行平均频率的表或类似的东西。
有没有一种方法可以确定每一行与其他行相比的唯一性?
1 2 3
A 100 B
A 200 B
A 200 B
就像上面的数据帧一样,我想说每一行都是唯一的
1 2 3
A 200 B
A 200 B
A 100 B
对于此数据集,第1行和第2行不是唯一的。我不想去掉一个,但我希望量化/加权非唯一行的数量。
问题是我的数据帧有14000000行长,所以我需要想一种方法来显示这么大的集合中每一行的唯一性。
假设您使用的是panda,这里有一种可能的方法:
import pandas as pd
# Setup, which you can probably skip since you already have the data.
cols = ["1", "2", "3"]
rows = [
["A", 200, "B",],
["A", 200, "B",],
["A", 100, "B",],
]
df1 = pd.DataFrame(rows, columns=cols)
# Get focus column values before adding a new column.
key_columns = df1.columns.values.tolist()
# Add a line column
df1["line"] = 1
# Set new column to cumulative sum of line values.
df1["match_count"] = df1.groupby(key_columns )['line'].apply(lambda x: x.cumsum())
# Drop line column.
df1.drop("line", axis=1, inplace=True)
打印结果
打印(df1(
输出-
1 2 3 match_count
0 A 200 B 1
1 A 200 B 2
2 A 100 B 1
仅返回唯一行:
# We only want results where the count is less than 2,
# because we have our key columns saved, we can just return those
# and not worry about 'match_count'
df_unique = df1.loc[df1["match_count"] < 2, key_columns]
print(df_unique)
输出-
1 2 3
0 A 200 B
2 A 100 B