- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 pandas Dataframe 的形式:
A B K S
2012-03-31 NaN NaN NaN 10
2012-04-30 62.74449 15.2 71.64 0
2012-05-31 2029.487 168.8 71.64 0
2012-06-30 170.7191 30.4 71.64 0
我尝试创建一个使用 df['S'][index-1] 值替换 df['S'] 的函数。
例如:
for index,row in df.iterrows:
if index = 1:
pass
else:
df['S'] = min(df['A'] + df['S'][index-1]?? - df['B'], df['K'])
但我不知道如何获取 df['S'][index - 1]
最佳答案
看起来您的初始答案非常接近。
以下应该有效:
for index, row in df.iterrows():
if df.loc[index, 'S'] != 0:
df.loc[index, 'S'] = df.loc[str(int(index) - 1), 'S']
基本上,对于除第一个索引之外的所有索引,即 0,将“S”列中的值更改为其前一行中的值。注意:这假设数据框具有顺序的、有序的索引。
iterrows()
方法不允许您通过自行调用行来修改值,因此您需要使用 df.loc()
来识别数据框中的单元格,然后更改它的值。
还值得注意的是 index
不是整数,因此使用 int()
函数减去 1。这都在 str 中()
函数,以便最终的索引输出是一个字符串,正如预期的那样。
关于具有先前值的 Python Pandas iterrows(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473153/
我编写了以下代码来创建数据框,并根据特定条件添加新的行和列。不幸的是,执行起来需要很多时间。 有其他方法可以做到这一点吗?任何意见都将受到高度赞赏。 dfCircuito=None for index
我有一个pandas数据框 from pandas import DataFrame, Series 其中每一行对应一个案例,每一列对应一个月份。我想在每 12 个月期间执行一次滚动求和。看起来很简单
我知道这个话题已经被讨论了一千次了。但我想不出解决办法。 我正在尝试计算列表(df1.list1 的每一行)在列表 (df2.list2) 的列中出现的频率。所有列表仅包含唯一值。 List1 包括大
我有这段代码,其中有大约 60k 行。完成整个过程大约需要 4 个小时。这段代码不可行,时间紧迫,想用apply代替iterrow。 这是代码, all_merged_k = pd.DataFrame
我有这段代码,其中有大约 60k 行。完成整个过程大约需要 4 个小时。这段代码不可行,时间紧迫,想用apply代替iterrow。 这是代码, all_merged_k = pd.DataFrame
我需要从 6 列数据帧的单行中选择 3 列,并将其附加到另一个只有 3 列的数据帧。 import pandas import numpy df1 = pd.DataFrame({'Name':['S
这个问题已经有答案了: Create Pandas DataFrame from txt file with specific pattern (6 个回答) 已关闭 4 年前。 我正在研究代表美国地
以下是示例数据 - 当前解决方案 - for idx, row in data.iterrows(): rp = row['right_player_test'] lp = row['
当我为每个客户创建新的数据框架时,我还想创建一个将所有这些数据框架附加在一起的巨大数据框架。 我创建了一个函数来按照我需要的方式对用户数据进行分组。现在我想迭代另一个包含唯一用户 key 的数据帧,并
d = {'2014-07-10 08:26:29.091199': {'Col':'12976482066142964'}, '2014-07-10 08:26:29.171198': {'Col'
我有一个数据集,其中包含几年来每天的美国国债曲线。行 = 日期,列 = 特定国债期限(3 个月、1 年、10 年等) 我有每天循环并校准利率模型参数的Python代码。我在通过 iterrows 和循
我有一个 pandas Dataframe 的形式: A B K S 2012-03-31 NaN NaN
我讨论了 iterrows 的性能问题 previously , 并得到了良好的普遍 react 。这个问题是一个特定的案例,我希望你能帮助我更好地应用一些东西,因为 iterrows 很慢。 我相信
我在 pandas 中有一个 df,它看起来像: id name values 1 a cat dog 2 b bird fly 我目前在做: for index, row
我相信有人也在 this thread 中报告过, 使用 iterrows() 填充数据框可能导致持久性问题。例如。像这样简单的东西: my_dataframe = pd.DataFrame(np.N
以下代码读取 csv 文件并遍历“子”列的行: df = pd.read_csv("Subs_test.csv", usecols= ['sub']) for index, row in df.ite
我是编程新手,到目前为止,这个问题已经“超出了我的薪水等级”,因此非常感谢您对此提供的任何帮助! 我有一个如下所示的 DataFrame 对象: Dan
我有两个数据集。 df Name Date Quantity ZMTD 2018-06-30 1000 ZMTD 2018-05-31 975 ZMT
我想知道在使用 iterrows 遍历 DataFrame 时如何使用索引而不是名称访问列。 这段代码是我能找到的最多的: for index, row in df.iterrows(): p
我有两个非常相似的 iterrows 循环,一个按我的预期工作,另一个不工作。我的问题是什么使第二个与第一个不同,为什么这会改变我的循环的行为?谢谢 工作循环 我正在使用数据框 drugs_tall
我是一名优秀的程序员,十分优秀!