如何检测 pandas 字符串列/series 中的值变化
TL;DR
为了获取每次输入字符串列变化时为 True 的 series,使用
example.py
my_column_changes = df["MyStringColumn"].shift() != df["MyStringColumn"]此 Series 的第一个值始终为 True,因为在 series 开始之前值被认为是 NaN(由于 shift() 的行为)。为了强制第一个值为 False,使用
example.py
my_column_changes.iloc[0] = False为了获取 dataframe 中列变化的行,使用
example.py
df[my_column_changes]或使用此单行命令:
example.py
df[df["MyStringColumn"].shift() != df["MyStringColumn"]]为了将此值分配给 DataFrame 中的新列,使用例如
example.py
df["MyStringColumnChanges"] = df["MyStringColumn"].shift() != df["MyStringColumn"]完整示例:
首先我们从上一篇关于如何创建 pandas 时间序列 DataFrame 示例数据集的文章中加载示例:
example.py
import pandas as pd
# 加载预构建的时间序列示例数据集
df = pd.read_csv("https://techoverflow.net/datasets/timeseries-example.csv", parse_dates=["Timestamp"])
df.set_index("Timestamp", inplace=True)现在我们创建一个新列,如果 "Sine" 列中的正弦波值为正则包含 Positive,如果该值为负则包含 "Negative":
example.py
df["SinePositive"] = (df["Sine"] >= 0).map({True: "Positive", False: "Negative"})现在我们使用上面显示的方法创建 ZeroCrossing 列:
example.py
# 创建 "change" 列(布尔值)
df["ZeroCrossing"] = df["SinePositive"].shift() != df["SinePositive"]…并将第一个条目设置为 False,因为我们不认为 series 的开始是零交叉:
example.py
df["ZeroCrossing"].iloc[0] = False现在我们可以使用
example.py
df[df["ZeroCrossing"]]来显示 DataFrame 中发生零交叉的行:
output.txt
Sine Cosine SinePositive ZeroCrossing
Timestamp
2020-05-25 20:05:10.040874 -6.283144e-03 -0.99998 Negative True
2020-05-25 20:05:10.090874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.140874 -6.283144e-03 -0.99998 Negative True
2020-05-25 20:05:10.190874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.240874 -6.283144e-03 -0.99998 Negative True
2020-05-25 20:05:10.290874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.340874 -6.283144e-03 -0.99998 Negative True
2020-05-25 20:05:10.390874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.440774 -2.450532e-15 -1.00000 Negative True
2020-05-25 20:05:10.490874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.540874 -6.283144e-03 -0.99998 Negative True
2020-05-25 20:05:10.590874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.640774 -1.960673e-15 -1.00000 Negative True
2020-05-25 20:05:10.690874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.740874 -6.283144e-03 -0.99998 Negative True
2020-05-25 20:05:10.790874 6.283144e-03 0.99998 Positive True
2020-05-25 20:05:10.840874 -6.283144e-03 -0.99998 Negative True
2020-05-25 20:05:10.890774 4.901063e-15 1.00000 Positive True
2020-05-25 20:05:10.940874 -6.283144e-03 -0.99998 Negative True完整示例代码:
example.py
import pandas as pd
# 加载预构建的时间序列示例数据集
df = pd.read_csv("https://techoverflow.net/datasets/timeseries-example.csv", parse_dates=["Timestamp"])
df.set_index("Timestamp", inplace=True)
# 创建包含 "Positive" 或 "Negative" 的新列
df["SinePositive"] = (df["Sine"] >= 0).map({True: "Positive", False: "Negative"})
# 创建 "change" 列(布尔值)
df["ZeroCrossing"] = df["SinePositive"].shift() != df["SinePositive"]
# 将第一个条目设置为 False
df["ZeroCrossing"].iloc[0] = False
# 打印结果
print(df[df["ZeroCrossing"]])If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow