Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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_Python 3.7 - Fatal编程技术网

Python for循环以获取熊猫中的数据帧

Python for循环以获取熊猫中的数据帧,python,pandas,python-3.7,Python,Pandas,Python 3.7,我尝试使用for循环获取数据帧中的某些数据。但是当我运行循环时,我得到的只是数据的索引,而不是行中显示的值 import pandas as pd import numpy as np data = {'time' : [1,2,3,4,5,6,7,8,9,10,11,12], 'values'[290,260,288,300,310,303,329,340,316,330,308,310]} df = pd.DataFrame(data) for i in df: print(

我尝试使用for循环获取数据帧中的某些数据。但是当我运行循环时,我得到的只是数据的索引,而不是行中显示的值

import pandas as pd
import numpy as np

data = {'time' : [1,2,3,4,5,6,7,8,9,10,11,12], 'values'[290,260,288,300,310,303,329,340,316,330,308,310]}

df = pd.DataFrame(data)

for i in df:

    print(i)
我只得到索引而不是值

我还尝试:

for index , values in df:

    print(values)
它给了我这个错误: 无法解压缩不可编辑的int对象


我知道iterrows会给我行,但我希望它作为一个完整的数据帧,而不是每行

尝试使用
dataframe.iterrows()

注意:
无论如何,我想说的是,不建议在熊猫中使用这种类型的操作,因为它是一种非矢量化的解决方案,与矢量化操作相比,它缺乏性能,我建议您在互联网上寻找矢量化。。。祝您好运:)

您还可以使用
df.values
数据帧上迭代

import pandas as pd
import numpy as np

data = {'time' : [1,2,3,4,5,6,7,8,9,10,11,12], 'values':[290,260,288,300,310,303,329,340,316,330,308,310]}

df = pd.DataFrame.from_dict(data)

>>> for i in df.values:
...     print(i)
...
[  1 290]
[  2 260]
[  3 288]
[  4 300]
[  5 310]
[  6 303]
[  7 329]
[  8 340]
[  9 316]
[ 10 330]
[ 11 308]
[ 12 310]

此外,您还可以根据列的
索引筛选所需行

尝试使用以下方法:

for ind in df.index:
     print(list(df[col][ind] for col in df.columns))
这段代码按行打印数据

for ind in df.index:
     print(list(df[col][ind] for col in df.columns))