Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用for循环根据另一列的值获取一列的值_Python_Pandas_For Loop - Fatal编程技术网

Python 使用for循环根据另一列的值获取一列的值

Python 使用for循环根据另一列的值获取一列的值,python,pandas,for-loop,Python,Pandas,For Loop,我试图根据另一列的值获取一列的所有值。 我已经发现了一些与我相关的有用的stackoverflow问题,但这些问题的解决方案似乎在可变范围内不起作用。我是否需要对变量执行不同的操作 我试图只从数据集中获取列“open”的值,其中“month”的值等于循环中的month变量 需要明确的是,预期输出仅为“打开”值 for year in dfClose['year'].unique(): tempYearDF = dfClose[dfClose['year'] == year]

我试图根据另一列的值获取一列的所有值。 我已经发现了一些与我相关的有用的stackoverflow问题,但这些问题的解决方案似乎在可变范围内不起作用。我是否需要对变量执行不同的操作

我试图只从数据集中获取列“open”的值,其中“month”的值等于循环中的month变量

需要明确的是,预期输出仅为“打开”值

for year in dfClose['year'].unique():
        tempYearDF = dfClose[dfClose['year'] == year]
        for month in range(1,13):
            tempOpenDF = tempYearDF.loc[tempYearDF['month'] == month, 'open']
我计划在分配数据后对tempOpenDF变量进行更多操作,但首先需要验证它是否正在填充

样本数据

dfClose

    open      year  month   day    date
0   30.490000   2010    1   4   2010-01-04
1   30.657143   2010    1   5   2010-01-05
2   30.625713   2010    1   6   2010-01-06
3   30.250000   2010    1   7   2010-01-07
4   30.042856   2010    1   8   2010-01-08
.
.
2551    297.260010  2020    2   24  2020-02-24
2552    300.950012  2020    2   25  2020-02-25
2553    286.529999  2020    2   26  2020-02-26
2554    281.100006  2020    2   27  2020-02-27
2555    257.260010  2020    2   28  2020-02-28
输出

tempOpenDF
Series([], Name: open, dtype: float64)
数据类型

tempYearDF.dtypes

open     float64
year       int64
month      int64
day        int64
date      object
dtype: object
“年”的所有数据都正确地分开了,只是现在抓取月份数据有困难

tempYearDF

    open    year    month   day date
2516    296.239990  2020    1   2   2020-01-02
2517    297.149994  2020    1   3   2020-01-03
2518    293.790009  2020    1   6   2020-01-06
2519    299.839996  2020    1   7   2020-01-07
2520    297.160004  2020    1   8   2020-01-08
2521    307.239990  2020    1   9   2020-01-09
2522    310.600006  2020    1   10  2020-01-10
2523    311.640015  2020    1   13  2020-01-13
2524    316.700012  2020    1   14  2020-01-14
2525    311.850006  2020    1   15  2020-01-15
2526    313.589996  2020    1   16  2020-01-16
2527    316.269989  2020    1   17  2020-01-17
2528    317.190002  2020    1   21  2020-01-21
2529    318.579987  2020    1   22  2020-01-22
2530    317.920013  2020    1   23  2020-01-23
2531    320.250000  2020    1   24  2020-01-24
2532    310.059998  2020    1   27  2020-01-27
2533    312.600006  2020    1   28  2020-01-28
2534    324.450012  2020    1   29  2020-01-29
2535    320.540009  2020    1   30  2020-01-30
2536    320.929993  2020    1   31  2020-01-31
2537    304.299988  2020    2   3   2020-02-03
2538    315.309998  2020    2   4   2020-02-04
2539    323.519989  2020    2   5   2020-02-05
2540    322.570007  2020    2   6   2020-02-06
2541    322.369995  2020    2   7   2020-02-07
2542    314.179993  2020    2   10  2020-02-10
2543    323.600006  2020    2   11  2020-02-11
2544    321.470001  2020    2   12  2020-02-12
2545    324.190002  2020    2   13  2020-02-13
2546    324.739990  2020    2   14  2020-02-14
2547    315.359985  2020    2   18  2020-02-18
2548    320.000000  2020    2   19  2020-02-19
2549    322.630005  2020    2   20  2020-02-20
2550    318.619995  2020    2   21  2020-02-21
2551    297.260010  2020    2   24  2020-02-24
2552    300.950012  2020    2   25  2020-02-25
2553    286.529999  2020    2   26  2020-02-26
2554    281.100006  2020    2   27  2020-02-27
2555    257.260010  2020    2   28  2020-02-28
如果我也使用一个实际值来表示相等,我会得到我想要的结果。 但当我尝试基于范围循环值使用该值时,它会中断

你就不能先把年复一年,然后再从那里开始吗

for _, v in df.groupby(['year', 'month'])['open']:
    tempOpenDF = v
    # do stuff
示例数据帧:

     0     1  2
0  123  2020  1
1  234  2020  2
2  543  2020  1
#适用于所有特殊年份
对于df[1]中的y,唯一()
#所有独特的月份
对于df[2]中的m,唯一()
#根据月份获取行
行=df.loc[df[2]==m]
#仅打印所需的列
打印(第[0]行)
输出:

0    123
2    543
Name: 0, dtype: int64
1    234
Name: 0, dtype: int64
loc
=位置或“命名”项目

您可能需要
iloc
,但是
tempYearDF['month']
不是一整列吗?
您可能需要参考
tempYearDF['month'].值
tempYearDF['month']。此列的名称(或任何适当的方法/属性)


df[df[“month”]==1]
是一个包含21行和所有列的切片
df.loc[df[“month”]==1]
也是一个包含21行和所有列的切片 “
df.loc[df[“month”]==1,“open”
会在month=1时返回
open
列中的21行

您也在哪里保存此文件?
tempOpenDF
位于
for
循环中的。它的值将随循环的每个索引而变化

我必须看到更多的信息被传递到哪里。就目前情况而言,您可以正确地过滤,但不会将过滤后的数据发送到任何地方

你所拥有的在其他方面是有用的

import pandas as pd
df = pd.read_csv("sample_data.csv",sep='\t',parse_dates=["date"])
# sample data is what you provided above, using tab separation
#

some_year = 2020
print(df.loc[df["month"] == 1, 'open'],'\n')
print(df.loc[df["year"] == 2020, 'open'],'\n')
# print(df.loc[(df["month"] == 1 and df["year"] == 2020), 'open'])

for i in range(1,13):
    dfy = df.loc[df["year"] == 2020]
    mondata = dfy.loc[dfy["month"] == i, "open"]
    print("Month: ",i,'\n',mondata,"\n")
>>df.head()

某些索引开放年-月-日日期

0 2516 296.23990 2020 1 2 2020-01-02

12517297.149994 2020 13 2020-01-03

22518293.790009 2020 1 6 2020-01-06

32519299.839996 2020 1 7 2020-01-07

4250297.160004 2020 1 8 2020-01-08


真正的索引是0,1等。
一些索引
来自您的数据。

您的预期输出是什么?只有开放值可以让您发布一个示例数据框,说明结果应该是什么样子的?我看到了中间数据框的示例,但没有明确的示例说明解决方案应该产生什么添加到问题的底部谢谢,这是非常有用的,我将进一步研究它,这对python来说还是一个新问题。我通过在month变量周围添加引号解决了这个问题。值得一提的是,您应该使用它,而不是使用循环解决方案。通过使用循环在数据帧上进行迭代,在弄乱了一堆sug之后,您将无法使用数据帧这里的手势,这似乎是最好的。谢谢大家!
for month in range(1,13):
    tempOpenDF = tempYearDF.loc[tempYearDF['month'] == month, 'open']
import pandas as pd
df = pd.read_csv("sample_data.csv",sep='\t',parse_dates=["date"])
# sample data is what you provided above, using tab separation
#

some_year = 2020
print(df.loc[df["month"] == 1, 'open'],'\n')
print(df.loc[df["year"] == 2020, 'open'],'\n')
# print(df.loc[(df["month"] == 1 and df["year"] == 2020), 'open'])

for i in range(1,13):
    dfy = df.loc[df["year"] == 2020]
    mondata = dfy.loc[dfy["month"] == i, "open"]
    print("Month: ",i,'\n',mondata,"\n")