1 回答

TA贡献1830条经验 获得超3个赞
您可以使用多种 Pandas 函数。基本上,您可以用来按内容过滤数据框的语法是:
df = df[(condition1) & (condition2) & ...] # filter the df and assign to the same df
专门针对您的情况,您可以替换condition为以下函数(表达式):
df[some_column] == some_value
df[some_column].isin(some_list_of_values) # This check whether the value of the column is one of the values in the list
df[some_column].str.contains() # You can use it the same as str.contains()
df[some_column].str.isdigit() # Same usage as str.isdigit(), check whether string is all digits, need to make sure column type is string in advance
df[some_column].str.len() == 4 # Filter string with length of 4
最后,如果要重置索引,可以使用df = df.reset_index(drop=True)将输出 df 索引重置为 0,1,2,...
编辑:要检查您可以使用的 NaN、NaT、None 值
df[some_column].isnull()
对于多列,您可以使用
df[[col1, col2]].isin(valuelist).all(axis=1)
添加回答
举报