简体   繁体   English

如何避免在 Pandas 数据帧上写一个粗的 for 循环

[英]how to avoid writing a gross for-loop on a pandas dataframe

I have a DataFrame like:我有一个 DataFrame 像:

import pandas as pd

begin_month = pd.Series([1, 19, 45, 32, 54])
end_month = pd.Series([19,45,32,54,99])

inventory = pd.DataFrame({"begin_month":begin_month, "end_month": end_month})

I want to make a third column, a boolean value, that says, "for each month, does the begin_month inventory == the previous month's end_month inventory level?"我想创建第三列,一个布尔值,表示“对于每个月,begin_month 库存是否 == 上个月的 end_month 库存水平?”

I can write a foul for-loop that does this, but am wondering how I could write a vectorized action to achieve the same thing.我可以编写一个错误的 for 循环来执行此操作,但我想知道如何编写矢量化操作来实现相同的目的。 Furthermore, the edge case is index location 0, for which there is nothing to compare its begin_month value to.此外,边缘情况是索引位置 0,没有什么可以比较它的 begin_month 值。

import pandas as pd

begin_month = pd.Series([1, 19, 145, 32, 54])
end_month = pd.Series([19,45,32,54,99])

df = pd.DataFrame({"begin_month":begin_month, "end_month": end_month})

df['parity'] = df['begin_month'] == df['end_month'].shift()
df.ix[0,'parity'] = True

print df

The key is to use .shift() so that you can compare the current row with an adjacent row.关键是使用 .shift() 以便您可以将当前行与相邻行进行比较。 and I set df.ix[0, 'parity'] = True because it has no predecessor to compare it to.并且我设置了 df.ix[0, 'parity'] = True 因为它没有可以与之比较的前身。

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

相关问题 使用 for 循环生成 pandas dataframe - Generate a pandas dataframe with for-loop 如何制作 Pandas 数据框 for 循环(用于股票市场 API) - How to make a pandas dataframe for-loop (for a stock market API) 如何避免大型数据集的Pandas DataFrame中的for循环 - How to avoid for loop in a Pandas DataFrame for large dataset Python:如何避免在熊猫数据框中进行循环转换? - Python: how to avoid loop to convert in a pandas dataframe? 使用日期时间的 pandas dataframe 中的部分 for 循环 - Partail for-loop in pandas dataframe using datetime 如何在不使用for循环的情况下基于来自另一个Dataframe的值来切片pandas DataFrame? - How to slice pandas DataFrame based on values from another Dataframe without using for-loop? 如何向量化注意力操作并避免for循环 - How to vectorize attention operation and avoid for-loop 如何使用 Python For 循环写入文件 - How to writing files using Python For-loop Python / Pandas:使用“for-loop”将多个Dataframes写入Excel工作表 - Python/Pandas: writing multiple Dataframes to Excel sheets using a “for-loop” 如何使用 for 循环创建一定长度的 numpy 数组(或 pandas 数据帧)? - How to create a numpy array (or pandas dataframe) of a certain length using for-loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM