python - Pandas 数据框按列下降

标签 python pandas drop

我想根据列中的值过滤数据框。这是 df 的外观:

    lead_snp Set_1 Set_2 Set_3 Set_4 Set_5  ... Set_4995 Set_4996 Set_4997 Set_4998 Set_4999 Set_5000
0  1:2444414     8     7     1    10    17  ...       16        6       10       12        8       12
1  1:1865298     2     2    11    21     6  ...       16        3       13       17        8        3
2  1:1865298     2     2    11    21     6  ...       16        3       13       17        8        3
3  1:1865298     2     2    11    21     6  ...       16        3       13       17        8        3
4  1:1865298     2     2    11    21     6  ...       16        3       13       17        8        3

当我运行 (lead_chrom_only_df.groupby("lead_snp").nunique().drop("lead_snp", axis=1)) 时,出现以下错误:

KeyError: "['lead_snp'] not found in axis"

不确定我是否遗漏了一些明显的东西,提前致谢。

最佳答案

尝试传递as_index = False

out =  lead_chrom_only_df.groupby("lead_snp",as_index = False).nunique().drop("lead_snp", axis=1)

关于python - Pandas 数据框按列下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69847272/

相关文章:

python - 如何删除数据帧(python)中具有起始索引和结束索引的一行索引?

python - 如何根据另一列的特定值从一列中删除 NaN

python - 我不应该能够填充通过变换导出的列吗?

python smtplib : Why is the connection refused?

python - pandas scatter_matrix 函数中 'ax' 关键字的用途

python - 使用 while 循环比较和修改 Series

python - 如何对 Excel 文件使用 .findall() 函数迭代列的所有行?

postgresql - TypeORM 迁移 :generate working great except for DROP

python - 字符串转datetime.datetime类型格式

python - 使用乘法生成列表是否会产生引用