Python中文
首页
教程
问答
标签
搜索
登录
注册
Pandas根据条件将一列映射到另一列
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个数据帧</p> <pre><code> game_id move_number move colour avg_centi phase 0 03gDhPWr 1 e4 white NaN opening 1 03gDhPWr 2 d5 black 37.0 opening 2 03gDhPWr 3 e5 white 61.0 opening 3 03gDhPWr 4 c5 black -5.0 opening 4 03gDhPWr 5 Nf3 white 26.0 opening ... ... ... ... ... ... ... 110093 zzaiRa7s 36 a5+ black NaN endgame 110094 zzaiRa7s 37 Kxb5 white NaN endgame 110095 zzaiRa7s 38 c6+ black NaN endgame 110096 zzaiRa7s 39 Ka4 white NaN endgame 110097 zzaiRa7s 40 Q@b4# black NaN endgame </code></pre> <p>我想映射<code>colour</code>列,这样当颜色为<code>black</code>时,<code>colour</code>列的值将替换为<code>phase</code>列中的值。我希望使用pandas <code>map</code>或<code>apply</code>函数来实现这一理想。不是<code>replace</code>太慢了。在</p> <p>生成的数据帧应如下所示:</p> ^{pr2}$ <p>我尝试过以下代码,但似乎不太管用:</p> <pre><code>def wrangle_game_phase(x): if x == 'black': return phase else: return x df['game_type'] = df['colour'].apply(wrangle_game_phase) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你试过了吗?loc…请试一下:</p> <p><code>df.loc[df['Color'] =='Black', 'Color'] = df['Phase']</code></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何用if条件捕获函数返回值
6 回答
如何用if语句判断列表中是否存在该索引?
8 回答
如何用if语句向量化numpy数组中的最大值?
1 回答
如何用IF语句有条件地保存零碎的结果?
4 回答
如何用if语句测试异常对象?
8 回答
如何用IF语句编写二元函数
10 回答
如何用igraph在python中创建顶点权重的图?
8 回答
如何用ijson和python解析json
9 回答
如何用iloc求子矩阵
5 回答
如何用Imagemagick或PIL绘制高质量的图像笔划(边框)?
9 回答
如何用importlib在python中动态导入模块?
5 回答
如何用import语句重写python内置函数?
1 回答
如何用imshow混合裁剪的强度并显示正确的混合强度?
6 回答
如何用in dictionary解析havin dictionary中的json文件
4 回答
如何用in-Django URL替换%20
3 回答
如何用in\op正确构造查询
10 回答
如何用inbuild对象替换文件
6 回答
如何用inheritan类实现flask restful
9 回答
如何用intersphinx正确地编写对外部文档的交叉引用?
9 回答
如何用int修改LpVariable?
6 回答