- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这就是我的数据的样子:
colA colB
a 1
a 1
c 2
c 2
Nan 1
c 1
a 2
Nan 2
我想把 Nans 填入 colA 中。结果应如下所示:
colA colB
a 1
a 1
c 2
c 2
a 1
c 1
a 2
c 2
第 5 行填充了“a”,因为 colB = 1 并且 colB 中的总体 1 已映射到比 colA 中的 c 更多的 a
第 8 行填充了“c”,因为 colB = 2 并且 colB 中的总体 2 已映射到比 colA 中的 a 更多的 c
最佳答案
您可以使用mode (忽略抽签)各组:
In [11]: df
Out[11]:
colA colB
0 a 1
1 a 1
2 c 2
3 c 2
4 NaN 1
5 c 1
6 a 2
7 NaN 2
In [12]: modes = df.groupby('colB')['colA'].transform(lambda x: x.mode().iloc[0])
In [13]: modes
Out[13]:
0 a
1 a
2 c
3 c
4 a
5 a
6 c
7 c
Name: colA, dtype: object
使用 fillna 仅替换 NaN 的模式:
In [14]: df['colA'].fillna(modes)
Out[14]:
0 a
1 a
2 c
3 c
4 a
5 c
6 a
7 c
Name: colA, dtype: object
In [15]: df['colA'] = df['colA'].fillna(modes)
<小时/>
注意:阅读文档,如果没有任何项目至少出现一次,则会引发此错误,因此您可能希望在转换中使用更强大的函数:
def mymode(s):
try:
return s.mode().iloc[0]
except IndexError:
# just pick the first element, even though it occurs only once, even if it's NaN
return s.iloc[0] if len(s) >= 1 else np.nan
关于python - 将数据从一列回填到另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116286/
设置 考虑 df = pd.DataFrame(index=pd.date_range('2016-03-01', '2016-03-08'), columns=list('ABC')) df.ilo
我经常遇到必须从另一个数据源“填写”信息的情况。 例如: x 2 b y 3 c x 4 d w 5 e v 6 f u 7 g t 8
所以这是场景: 我们有一个 m_reading 表,每天都会在其中提取 24 个读数。 在 24 个读数中,23 个是当天的读数,1 个是前一天的读数。 物化 View 应该聚合天数数据。 业务用户可
假设您有一个 Airflow DAG,回填没有意义,这意味着,在它运行一次之后,再快速运行它之后的时间将完全没有意义。 例如,如果您从某个仅每小时更新一次的来源将数据加载到数据库中,那么快速连续发生的
我有一个如下所示的 DAG,dagrun_timeout 设置为 4 小时。当我转到 UI 并清除从 start_date 到 present 的所有任务时,所有 DAG 都设置为运行状态。 4 小时
我的第一篇文章。我在 Google Analytics(分析)中的目标漏斗中遇到了大问题。 我们设置了一个漏斗来跟踪从我们的主要公司网站到在第三方网站上处理的用户注册的注册(使用虚拟网页浏览来跟踪用户
试图找出一种方法来回填 ds 分区 Hive 表的分区。 我知道如何从 CLI 运行 Hive 命令,例如 $HIVE_HOME/bin/hive -e 'select a.col from tab1
https://cloud.google.com/bigquery/docs/reference/datatransfer/rest/ 我正在寻找“bigquery 数据传输服务”的 php 客户端库
我是一名优秀的程序员,十分优秀!