我有这个数据框,我的目标是删除所有条目少于 1000 的列。
在旋转 df 之前,我知道我有 880 个唯一的 well_id,条目范围从 4 到 60k+。我知道最终应该有 102 个 well_id。
我试图以一种非常幼稚的方式完成此操作,方法是收集我试图在数组中删除的井并使用循环,但我不断收到“类型错误:级别类型不匹配”的消息,但是当我只使用 del 而没有for 循环它有效。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
char *ID;
char *name;
char *gender;
char *DOB;
int age;
char *faculty;
float gpa;
char *aca_rating;
} Student;
void read_file(const char *, int);
int main()
{
read_file("stackoverflow.txt", 2);
return 0;
}
感谢任何帮助,谢谢。
答案 0 :(得分:2)
您可以使用 dropna
方法:
df.dropna(thresh=[]) #specify [here] how many non-na values you require to keep the row
这种方法的优点是不需要创建列表。
如果您希望进行适当的更改,也不要忘记添加通常的 inplace = True
。
答案 1 :(得分:1)
您可以使用 pandas drop
方法:
df.drop(columns=['colName'], inplace=True)
你实际上可以传递一个列名列表:
unwanted_id = [164301.0, 'TB-0071']
df.drop(columns=unwanted_ids, inplace=True)
示例:
df[:5]
from to freq
0 A X 20
1 B Z 9
2 A Y 2
3 A Z 5
4 A X 8
df.drop(columns=['from', 'to'])
freq
0 20
1 9
2 2
3 5
4 8
要获得具有超过 1000 个唯一值的列名称,您可以使用以下方法:
counts = df.nunique()[df.nunique()>1000].to_frame('uCounts').reset_index().rename(columns={'index':'colName'})
计数
colName uCounts
0 to 1001
1 freq 1050