I want to select only rows that have fc_id == 2, and then delete those having duplicates. This is my input file enter image description here

我只是被困在了第一步.之后,我还需要一个输出文件,在其中我将获得fc_id==2且没有重复项的最终数据.

我试过这个:

df = pd.read_csv(r'test.csv')
df2 = df[df["fc_id"]==2]

def condi(df2):
    df3[x] = np.where(df(df2)==2, 1, 0)
    return x
var = condi(df2)
#print(var)

with open('test.csv', 'r') as in_file, open('out_test.csv', 'w') as out_file:
    seen = set()
    if var == 1:
         for line in in_file:
            if line in seen: continue

            seen.add(line)
            out_file.write(line)

我收到一个错误,当我试图打印(Var)时,它说"‘DataFrame’对象不可调用".

推荐答案

就像这样:

df = pd.read_csv(r'test.csv')
df2 = df[df['fc_id'] == 2]
df2.drop_duplicates(inplace=True)

Python相关问答推荐

调查TensorFlow和PyTorch性能的差异

如何判断LazyFrame是否为空?

除了Python之外,可以替代bare?

如何在超时的情况下同步运行Matplolib服务器端?该过程随机挂起

Python panda拆分列保持连续多行

如何使用Python中的clinicalTrials.gov API获取完整结果?

Odoo -无法比较使用@api.depends设置计算字段的日期

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

如何标记Spacy中不包含特定符号的单词?

为什么sys.exit()不能与subproccess.run()或subprocess.call()一起使用

django禁止直接分配到多对多集合的前端.使用user.set()

OR—Tools CP SAT条件约束

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

在np数组上实现无重叠的二维滑动窗口

python中字符串的条件替换

在Django admin中自动完成相关字段筛选

如何从列表框中 Select 而不出错?

从Windows Python脚本在WSL上运行Linux应用程序

如果初始groupby找不到满足掩码条件的第一行,我如何更改groupby列,以找到它?

跳过嵌套JSON中的级别并转换为Pandas Rame