简体   繁体   English

在 for 循环中创建多个列 python

[英]creating multiple columns in a for loop python

I'm new to Python. I'm trying to create multiple columns in a for loop but I'm having trouble with it.我是 Python 的新手。我正在尝试在 for 循环中创建多个列,但我遇到了麻烦。 I have several columns and I'm trying to create a new column that shows whether or not the elements in ohlcs is greater than elements in metrics.我有几个列,我正在尝试创建一个新列来显示 ohlcs 中的元素是否大于 metrics 中的元素。 I can do it to create one column but I want to save time since I plan on doing the same function but for different variables.我可以这样做来创建一个列,但我想节省时间,因为我计划对不同的变量执行相同的 function。

ohlcs = ['open', 'high', 'low', 'close']
metrics = ['vwap', '9EMA', '20EMA']
wip = []
for idx, row in master_df.iterrows():
    for ohlc in ohlcs:
        for metric in metrics:
            row[f'{ohlc} above {metric}'] = np.where(row[ohlc] >= row[metric], 1, 0)

This didn't do anything.这没有做任何事情。 I've also done this:我也这样做过:

ohlcs = ['open', 'high', 'low', 'close']
metrics = ['vwap', '9EMA', '20EMA']
wip = []
for idx, row in master_df.iterrows():
    for ohlc in ohlcs:
        for metric in metrics:
           if master_df[ohlc] >= master_df[metric]: 
               master_df[f'{ohlc} above {metric}'] = 1
           else:
               master_df[f'{ohlc} above {metric}'] = 0

That gave me an error.那给了我一个错误。

ValueError: The truth value of a Series is ambiguous. ValueError:Series 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

I did other things but I erased those as I worked on it.我做了其他事情,但我在处理它时删除了它们。 At this point I'm out of ideas.在这一点上我没主意了。 Please help!请帮忙!

I got it now but I checked manually to see if the values lined up and it wasn't.我现在明白了,但我手动检查了值是否对齐,但事实并非如此。

enter image description here在此处输入图像描述

How do I fix it?我如何解决它?

There is no need to iterate over the rows of the dataframe. This will give you the required result:无需遍历 dataframe 的行。这将为您提供所需的结果:

for ohlc in ohlcs:
    for metric in metrics:
        master_df[f'{ohlc} over {metric}'] = (master_df[ohlc] >= master_df[metric]).astype(int)

The part astype(int) is just to convert True and False into 1 and 0 , if you are okay with True and False representation, you can use just master_df[f'{ohlc} over {metric}'] = master_df[ohlc] >= master_df[metric] . astype(int)部分只是将TrueFalse转换为10 ,如果你对TrueFalse表示没问题,你可以只使用master_df[f'{ohlc} over {metric}'] = master_df[ohlc] >= master_df[metric]

EDIT: Of course, (master_df[ohlc] >= master_df[metric]).astype(int) is equivalent to np.where(master_df[ohlc] >= master_df[metric], 1, 0) , you can use either.编辑:当然, (master_df[ohlc] >= master_df[metric]).astype(int)等同于np.where(master_df[ohlc] >= master_df[metric], 1, 0) ,您可以使用其中任何一个。

Consider itertools.product and the functional form DataFrame.ge for all pairwise possibilities fir a flatter looping:考虑itertools.product和函数形式DataFrame.ge以获得更平坦循环的所有成对可能性:

from itertools import product
...

ohlcs = ['open', 'high', 'low', 'close']
metrics = ['vwap', '9EMA', '20EMA']

pairs = product(ohlcs, metrics)

for ohlc, metric in pairs:
    master_df[f"{ohlc} over {metric}"] = master_df[ohlc].ge(master_df[metric])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM