Python中文
首页
教程
问答
标签
搜索
登录
注册
Pandas使用哪种Python魔术方法?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个包含熊猫数据帧的类(self.my\u数据框),并更新self.my\u数据框没有我预期的效果。下面是代码的简化版本,说明了我的问题:</p> <pre><code>class my_obj(object): @property def my_df(self): if not hasattr(self, "_my_df"): self._my_df = pandas.DataFrame({ "A" : [1,2,3,], "B" : [4,5,6]}).fillna("") print("Retrieving!") return self._my_df @my_df.setter def my_df(self, my_new_df): print("Setting!") self._my_df = my_new_df.copy() </code></pre> <p>下面是我(尝试)调用这些方法时发生的情况(从一个单独的实例方法内部,我认为在这里不重要):</p> <pre><code>ipdb> self.my_df Retrieving! A B 0 1 4 1 2 5 2 3 6 ipdb> self.my_df.loc[2, "B"] = "x" Retrieving! ipdb> self.my_df Retrieving! A B 0 1 4 1 2 5 2 3 x ipdb> self._my_df A B 0 1 4 1 2 5 2 3 x </code></pre> <p>我希望<code>self.my_df.loc[2, "B"] = "x"</code>调用setter,但它没有调用,或者——如果它<em>没有</em>——那么我希望self。你知道吗</p> <p>这里发生了什么?我的真实情况要复杂得多,但我相信这是我困惑的根源。你知道吗</p> <p>谢谢你帮我弄清楚这件事。你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>如果/当您<strong><em>设置</em></strong><code>self.my_df</code>时,将调用setter。你知道吗</p> <p>也就是说,如果你做了<code>self.my_df = 2</code>,那就是setter被调用的时候。你知道吗</p> <p>您要做的是修改与<code>self.my_df</code>关联的对象。所以你的代码就是获取数据帧,然后修改它。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
区分轴上的像素大小
10 回答
区分过度拟合与良好预测
1 回答
区分通过POST接收的数据并在Cherrypy中获取
10 回答
区分重写和重载
3 回答
区分金贾二号的类型
2 回答
区别:IF IN和IF==python
6 回答
区别:list.append()与incrementequal?
10 回答
区别:创建在启动wx应用程序时不“阻止”控制台的脚本
5 回答
区别于python中的命令行和CGI
9 回答
区别关系
5 回答
区别是什么APIClient.enforce\ csrf\检查以及APIClient.handler.enforce\ csrf\ u检查
5 回答
区块.draw()未在python空闲模式下工作
10 回答
区块链API调用输出对象名称而不是内容
7 回答
区块链nonce值随负载变化
6 回答
区块链Python语法错误(Snakecoin)
6 回答
区块链不连接到服务器和矿工
7 回答
区块链信息钱包支票支付员
10 回答
区块链和区块链密码错误
10 回答
区块链尝试向Transaction字典添加键/值以创建区块链
3 回答
区域:IOError:[Errno 22]无效模式('w')或filenam
9 回答