选择数据 - df[df["A"] == "a"]] - 列A等于a的数据
筛选列A等于a的数据
import numpy as np
import pandas as pd
df = pd.DataFrame(data=[{"A":"1", "B":"3", "C":"4"}, {"A":"1", "B":"2", "C":"5"}, {"A":"2", "B":"1", "C":"6"}])
df = pd.DataFrame(data=[{"A":1, "B":3, "C":4}, {"A":1, "B":2, "C":5}, {"A":2, "B":1, "C":6}])
|
A |
B |
C |
0 |
1 |
3 |
4 |
1 |
1 |
2 |
5 |
2 |
2 |
1 |
6 |
## A列的值是a的全部数据
df[df["A"] == 1]
## pandas取出包含某个值的所有行
df = df[df["from_account"].str.contains("fcwhx")]
## pandas取出不包含某个值的所有行
df = df[~df["from_account"].str.contains("不要的文字1", "不要的文字2")]
或
df = df[df["from_account"].str.contains("fcwhx")==False]
## 如果有空值,报错:TypeError: bad operand type for unary ~: 'float'
则用这个方法
df.other_group = df.other_group.fillna('####@')
df2 = df[~df['other_group'].str.contains('####@')]