Python中文
首页
教程
问答
标签
搜索
登录
注册
循环并修改列的每个元素
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>给定这样的数据集</p> <pre><code>import pandas as pd d = {'description': ["abcd","efgh","ijkl"], 'code': ["A","K","Z"]} df = pd.DataFrame(data=d) description code 0 abcd A 1 efgh K 2 ijkl Z </code></pre> <p>我的目标是得到这个</p> <pre><code> description code 0 "abcd" A 1 "efgh" K 2 "ijkl" Z </code></pre> <p>这段代码只打印出我的目标</p> <pre><code>for row in df["description"]: #accessing to each row print(f'"{row}"') #"modifying" each row </code></pre> <p>这就是产生的原因</p> <pre><code>"abcd" "efgh" "ijkl" </code></pre> <p>我试图设置这样的东西,但它是不正确的</p> <pre><code>rows = df.shape[0] i = 0 while i < rows: f'"{df.loc[i,"description"]}"' #wrong </code></pre> <p>或者类似的</p> <pre><code>df1["description"]=df1["description"].apply(lambda ?? </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>通过行迭代,您可以执行以下操作:</p> <pre><code>for i, row in df.iterrows(): new_val = something if <condition>: new_val = something_else df.at[i,'description'] = new_val </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在Excel中读取公式并将其转换为Python中的计算?
6 回答
如何在excel中读取嵌入的excel,并将嵌入文件中的信息存储在主excel文件中?
3 回答
如何在Excel中返回未知列长度的非空顶行列值?
7 回答
如何在excel中选择数据列?
2 回答
如何在Excel中通过脚本自动为一列中的所有单元格创建公共别名
4 回答
如何在excel中高效格式化范围AttributeError:“tuple”对象没有属性“fill”
10 回答
如何在excel单元格中编写python函数
6 回答
如何在excel单元格中自动执行此python代码?
3 回答
如何在excel工作表中创建具有相应值的新列
6 回答
如何在Excel工作表中复制条件为单元格颜色的python数据框?
9 回答
如何在Excel工作表中循环
3 回答
如何在excel工作表中打印嵌套词典?
5 回答
如何在excel工作表中绘制所有类的继承树?
8 回答
如何在Excel工作表中自动调整列宽?
1 回答
如何在excel工作表中追加并进一步处理
5 回答
如何在excel工作表之间进行更改?
9 回答
如何在excel或csv上获取selenium数据?
3 回答
如何在Excel或Python中将正确的值赋给正确的列
8 回答
如何在excel或python中提取单词周围的文本?
9 回答
如何在excel或python中转换来自Jira的3w 1d 4h的fromat数据?
6 回答