Python 熊猫:如何用计数器删除不符合特定条件的行?
使用大致如下所示的数据集:Python 熊猫:如何用计数器删除不符合特定条件的行?,python,pandas,Python,Pandas,使用大致如下所示的数据集: index_right = pd.Series([8620, 8620, 8620, 8650, 8650, 8650, 133000, 133000, 133000]) index_left = pd.Series([0, 0, 0, 1, 1, 1, np.nan, np.nan, np.nan]) month_index = pd.Series([1, 1, 1, 2, 2, 2, np.nan, np.nan, np.nan]) time_index = pd
index_right = pd.Series([8620, 8620, 8620, 8650, 8650, 8650, 133000, 133000, 133000])
index_left = pd.Series([0, 0, 0, 1, 1, 1, np.nan, np.nan, np.nan])
month_index = pd.Series([1, 1, 1, 2, 2, 2, np.nan, np.nan, np.nan])
time_index = pd.Series([1, 2, 3, 1, 2, 3, 1, 2, 3])
dict1 = {'index_right': index_right, 'index_left': index_left, 'month_index': month_index, 'time_index': time_index}
df = pd.DataFrame(dict1)
df
index_right index_left month_index time_index
8620 0.0 1.0 1.0
8650 1.0 2.0 2.0
133000 NaN NaN 1.0
133000 NaN NaN 2.0
133000 NaN NaN 3.0
我想检查每个左索引
,并删除与月索引
和时间索引
不匹配的任何行
结果应该如下所示:
index_right = pd.Series([8620, 8620, 8620, 8650, 8650, 8650, 133000, 133000, 133000])
index_left = pd.Series([0, 0, 0, 1, 1, 1, np.nan, np.nan, np.nan])
month_index = pd.Series([1, 1, 1, 2, 2, 2, np.nan, np.nan, np.nan])
time_index = pd.Series([1, 2, 3, 1, 2, 3, 1, 2, 3])
dict1 = {'index_right': index_right, 'index_left': index_left, 'month_index': month_index, 'time_index': time_index}
df = pd.DataFrame(dict1)
df
index_right index_left month_index time_index
8620 0.0 1.0 1.0
8650 1.0 2.0 2.0
133000 NaN NaN 1.0
133000 NaN NaN 2.0
133000 NaN NaN 3.0
我的方法是for循环并在适当的位置删除索引位置,但我正在努力解决它(目前不起作用):
用于检查序列是否相等:
res = df[df['month_index'].eq(df['time_index'])]
print(res)
输出
index_left month_index time_index
0 0 1 1
4 1 2 2
index_right index_left month_index time_index
0 8620 0.0 1.0 1
4 8650 1.0 2.0 2
6 133000 NaN NaN 1
7 133000 NaN NaN 2
8 133000 NaN NaN 3
还可以做以下工作:
res = df[df['month_index'] == df['time_index']]
更新
对于新示例,还需要检查NAN值:
输出
index_left month_index time_index
0 0 1 1
4 1 2 2
index_right index_left month_index time_index
0 8620 0.0 1.0 1
4 8650 1.0 2.0 2
6 133000 NaN NaN 1
7 133000 NaN NaN 2
8 133000 NaN NaN 3
我会解决我的问题,这确实有效。实际上,我正在尝试删除不符合条件的行。原因是,我有一个很长的时间序列结构,我想保持。因此,在使用左连接连接两个数据集时,我有额外的数据映射到需要删除的每一行。@JohnStud输出是什么?更新了示例,以保持仍保留的行useful@JohnStud更新了答案!太棒了,还是说得通。