简体   繁体   English

如何从循环中的第一列中减去第一列之后的列?

[英]How to subtract the columns after the first column, from the first column in a loop?

It seems simple but I can't seem to find an efficient way to solve this in Python 3: Is there is a loop I can use in my dataframe that subtracts every column after the first column, from the first column, so that I can add that new subtracted column to a new dataframe?看起来很简单,但我似乎无法在 Python 3 中找到解决此问题的有效方法:我的 dataframe 中是否有一个循环可以从第一列中减去第一列之后的每一列,这样我就可以将新减去的列添加到新的 dataframe?

Then I would like to move on to subtract every column after the second column, from the second column, and follow the same logic throughout the 18 columns where I append or add that new subtracted column to the new dataframe然后我想继续从第二列中减去第二列之后的每一列,并在我 append 的 18 列中遵循相同的逻辑,或者将新减去的列添加到新的 dataframe

Here are first 4 lines of code for the 1st and 2nd columns I am using to my dataframe (spotrates), but I have 18 columns and I know it would be easier to create a loop, and I am adding on to the end of my existing dataframe, when I want the subtracted column to be inserted to a new one.这是我用于 dataframe (spotrates) 的第一列和第二列的前 4 行代码,但我有 18 列,我知道创建循环会更容易,我将添加到我的末尾现有的 dataframe,当我希望将减去的列插入新列时。

spotrates['3m-on'] = spotrates.iloc[:,1] - spotrates.iloc[:,0]
spotrates['6m-on'] = spotrates.iloc[:,2] - spotrates.iloc[:,0]
spotrates['9m-on'] = spotrates.iloc[:,3] - spotrates.iloc[:,0]
spotrates['1y-on'] = spotrates.iloc[:,4] - spotrates.iloc[:,0]

spotrates['6m-3m'] = spotrates.iloc[:,2] - spotrates.iloc[:,1]
spotrates['9m-3m'] = spotrates.iloc[:,3] - spotrates.iloc[:,1]
spotrates['1y-3m'] = spotrates.iloc[:,4] - spotrates.iloc[:,1]
spotrates['2y-3m'] = spotrates.iloc[:,5] - spotrates.iloc[:,1]

Here is a code I've started to work on inefficiently:这是我开始低效处理的代码:

def swaps(data):
    i<len(data.columns)
    col1 = data.iloc[:,i]
    col2 = data.iloc[:,i+1]
    for col1, col2 in data.columns:
        return col2 - col1
        
        

I think you can use a (if you have a row for an id you can get your items by) select from where statement.我认为您可以使用 where 语句中的 select (如果您有一行 id 可以获取您的项目)。 make sure the 1 is whatever your first item's id is.确保 1 是您的第一个项目的 ID。

DELETE FROM mytable WHERE ID!=1

This will remove everything except the first row!这将删除除第一行之外的所有内容!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数组的每一列中减去第一列 - How to subtract the first column from every column of an array 从另一列中减去一个数据框(第一列Pandas除外) - Subtract one dataframe from another excluding the first column Pandas 如何从文件中减去每个其他值的每个第一列值? - How to subtract every first column value form every other values from a file? 用它的第一个值减去熊猫数据框中的一列 - Subtract a column in pandas dataframe by its first value 如何从第一列开始依次对DataFrame列进行排序? - How to sort DataFrame columns sequently from the first column? Python:如何在嵌套循环的第一列中进行搜索? - Python: How to search in first column of nested loop? 熊猫分组并用另一列的第一个值减去一列的最后一个值 - pandas groupby and subtract last value of one columns with first value of another column python迭代/循环两列并在第一次在a列或b列中找到值后删除整行 - python iterate / loop over two columns and drop entire row after value is first found in either column a or column b 如何将两个pandas列混合到一个数据框中,其中第一列的第一个元素,第二列的第二个元素等等? - How to mix two pandas columns into one dataframe with first element from first column, second element from second column and so on? 如何从pandas中的另一列中减去字符串类型列值 - how to subtract string type columns values from another column in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM