如何在 pandas 数据框中编写 sql 查询
How to write sql query in pandas dataframe
如何在 pandas 数据帧中编写以下查询
SELECT * FROM Table
WHERE a = "S"
AND b IS NOT NULL
AND c IS NOT NULL;
如果您正在寻找一个等效的命令来在 pandas 数据帧中获得相同的结果,您可以试试这个
df = pd.DataFrame({'a':['S','T','S'],'b':[None, 1, 2],'c':[1,2,3]})
df = df.dropna(subset=['b','c'])
df.loc[df.dropna(subset=['b','c'])['a'] == 'S']
原始数据框
a b c
0 S NaN 1
1 T 1.0 2
2 S 2.0 3
输出
a b c
2 S 2.0 3
如何在 pandas 数据帧中编写以下查询
SELECT * FROM Table
WHERE a = "S"
AND b IS NOT NULL
AND c IS NOT NULL;
如果您正在寻找一个等效的命令来在 pandas 数据帧中获得相同的结果,您可以试试这个
df = pd.DataFrame({'a':['S','T','S'],'b':[None, 1, 2],'c':[1,2,3]})
df = df.dropna(subset=['b','c'])
df.loc[df.dropna(subset=['b','c'])['a'] == 'S']
原始数据框
a b c
0 S NaN 1
1 T 1.0 2
2 S 2.0 3
输出
a b c
2 S 2.0 3