pandas 保留一列中包含某个值的数据框行,而该值在另一列中有多个匹配项

dzhpxtsq  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(128)

保留一列中包含某个值的数据框行,该值在另一列中有多个匹配项。
如果number列在letter列中有多个匹配项,则保留行。

number, letter

1, A

2, B

5, D

2, D

2, D

5, D

字符串
预期输出:

number, letter
2, B
2, D
2, D

qojgxg4l

qojgxg4l1#

import pandas as pd

data = {
    'number': [1, 2, 5, 2, 2, 5],
    'letter': ['A', 'B', 'D', 'D', 'D', 'D']
}
df = pd.DataFrame(data)

# Count the occurrences of each number in the "number" column
count_per_number = df['number'].value_counts()

# Filter the DataFrame by keeping only the rows where the number has multiple matches in the "letter" column
result_df = df[df['number'].isin(count_per_number[count_per_number > 1].index)]

print(result_df)

字符串
输出量:

number letter
1       2      B
3       2      D
4       2      D

相关问题