由于不熟悉 Pandas 也不了解其 API,我需要从另一个程序中获取一些数据并查询数据以获取一些信息。
Pandas 数据框中的列由交替的负值和正值块组成。我将使用什么查询来查找紧随一个或多个负值之后的第一个正值的所有索引以及紧随正值块之后的第一个负值的索引?
您可以找到每一行的符号,检查它是否与前一行不同并过滤您的结果。
import pandas as pd
import numpy as np
# Mock data
df = pd.DataFrame(
{
"col1": [1, 2, 3, -1, -2, 4, 5, -10]
}
)
# Find the sign of each row
df["signs"] = np.sign(df["col1"])
# Check if the sign is different from the previous one
# Will return 1 for a shift from negative to positive,
# -1 for a shift from positive to negative and NaN if
# the sign remains the same
df["flips"] = df["signs"].where(df["signs"] != df.signs.shift())
# Select negative to positive flips
df_negative_to_positive = df.loc[df["flips"] == 1]
# Select positive to negative flips
df_positive_to_negative = df.loc[df["flips"] == -1]
print(df, '\n\n', df_negative_to_positive, '\n\n', df_positive_to_negative)
# Output
# col1 signs flips
# 0 1 1 1.0
# 1 2 1 NaN
# 2 3 1 NaN
# 3 -1 -1 -1.0
# 4 -2 -1 NaN
# 5 4 1 1.0
# 6 5 1 NaN
# 7 -10 -1 -1.0
# col1 signs flips
# 0 1 1 1.0
# 5 4 1 1.0
# col1 signs flips
# 3 -1 -1 -1.0
# 7 -10 -1 -1.0