pandas 带条件的数据框组
pandas dataframe group with condition
我有一个 3D 数据框,其中 x 和 y 以及时间作为第 3 个维度。
数据是在不同时间拍摄的 5 英寸卫星图像。
x 和 y 描述了每个像素。
x y time SIPI classif
7.620001 -77.849990 2018-04-07 1.011107 2.0
2018-10-14 1.023407 2.0
2018-12-28 0.045107 3.0
2020-01-10 0.351107 2.0
2018-06-29 0.351107 2.0
-77.849899 2018-04-07 1.010777 8.0
2018-10-14 0.510562 2.0
2018-12-28 1.410766 4.0
2020-01-10 1.010666 8.0
2018-06-29 2.057068 8.0
-77.849809 2018-04-07 0.986991 1.0
2018-10-14 0.986991 8.0
2018-12-28 0.986991 5.0
2020-01-10 0.984791 5.0
2018-06-29 0.986991 3.0
-77.849718 2018-04-07 0.975965 10.0
2018-10-14 0.964765 7.0
2018-12-28 0.975965 10.0
2020-01-10 0.975965 10.0
2018-06-29 0.975965 3.0
-77.849627 2018-04-07 1.957747 2.0
2018-10-14 0.132445 6.0
2018-12-28 0.589677 2.0
2020-01-10 1.982445 2.0
2018-06-29 3.334456 7.0
我需要对数据进行分组,作为新列,我需要来自列 'classif_rf' 的值,这在 5 个数据集中最常见。这些值是 1 到 10 之间的整数。我想添加一个条件,它只添加高于 3 的频率。
x y classif
7.620001 -77.849990 2.0
-77.849899 8.0
-77.849809 Na
-77.849718 10.0
-77.849627 2.0
因此,我需要数据帧,其中每个像素都有一个频率最高的值,当频率低于 3 时,应该有一个 NA 值。
pandas.groupby 函数可以做到吗?我考虑过 value_counts(),但我不确定如何将其应用到我的数据集。
提前致谢!
这是一个笨拙的方法:
# Get the modes per group and count how often they occur
df_modes = df.groupby(["x", "y"]).agg(
{
'classif': [lambda x: pd.Series.mode(x)[0],
lambda x: sum(x == pd.Series.mode(x)[0])]
}
).reset_index()
# Rename the columns to something a bit more readable
df_modes.columns = ["x", "y", "classif_mode", "classif_mode_freq"]
# Discard modes whose frequency was less than 3
df_modes.loc[df_modes["classif_mode_freq"] < 3, "classif_mode"] = np.nan
现在 df_modes.drop("classif_mode_freq", axis=1)
将 return
x y classif_mode
0 7.620001 -77.849990 2.0
1 7.620001 -77.849899 8.0
2 7.620001 -77.849809 NaN
3 7.620001 -77.849718 10.0
4 7.620001 -77.849627 2.0
我有一个 3D 数据框,其中 x 和 y 以及时间作为第 3 个维度。 数据是在不同时间拍摄的 5 英寸卫星图像。 x 和 y 描述了每个像素。
x y time SIPI classif
7.620001 -77.849990 2018-04-07 1.011107 2.0
2018-10-14 1.023407 2.0
2018-12-28 0.045107 3.0
2020-01-10 0.351107 2.0
2018-06-29 0.351107 2.0
-77.849899 2018-04-07 1.010777 8.0
2018-10-14 0.510562 2.0
2018-12-28 1.410766 4.0
2020-01-10 1.010666 8.0
2018-06-29 2.057068 8.0
-77.849809 2018-04-07 0.986991 1.0
2018-10-14 0.986991 8.0
2018-12-28 0.986991 5.0
2020-01-10 0.984791 5.0
2018-06-29 0.986991 3.0
-77.849718 2018-04-07 0.975965 10.0
2018-10-14 0.964765 7.0
2018-12-28 0.975965 10.0
2020-01-10 0.975965 10.0
2018-06-29 0.975965 3.0
-77.849627 2018-04-07 1.957747 2.0
2018-10-14 0.132445 6.0
2018-12-28 0.589677 2.0
2020-01-10 1.982445 2.0
2018-06-29 3.334456 7.0
我需要对数据进行分组,作为新列,我需要来自列 'classif_rf' 的值,这在 5 个数据集中最常见。这些值是 1 到 10 之间的整数。我想添加一个条件,它只添加高于 3 的频率。
x y classif
7.620001 -77.849990 2.0
-77.849899 8.0
-77.849809 Na
-77.849718 10.0
-77.849627 2.0
因此,我需要数据帧,其中每个像素都有一个频率最高的值,当频率低于 3 时,应该有一个 NA 值。
pandas.groupby 函数可以做到吗?我考虑过 value_counts(),但我不确定如何将其应用到我的数据集。
提前致谢!
这是一个笨拙的方法:
# Get the modes per group and count how often they occur
df_modes = df.groupby(["x", "y"]).agg(
{
'classif': [lambda x: pd.Series.mode(x)[0],
lambda x: sum(x == pd.Series.mode(x)[0])]
}
).reset_index()
# Rename the columns to something a bit more readable
df_modes.columns = ["x", "y", "classif_mode", "classif_mode_freq"]
# Discard modes whose frequency was less than 3
df_modes.loc[df_modes["classif_mode_freq"] < 3, "classif_mode"] = np.nan
现在 df_modes.drop("classif_mode_freq", axis=1)
将 return
x y classif_mode
0 7.620001 -77.849990 2.0
1 7.620001 -77.849899 8.0
2 7.620001 -77.849809 NaN
3 7.620001 -77.849718 10.0
4 7.620001 -77.849627 2.0