Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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 dataframe我可以正确提取列表中的纯值吗?_Python_Pandas_Dataframe_Slice - Fatal编程技术网

Python dataframe我可以正确提取列表中的纯值吗?

Python dataframe我可以正确提取列表中的纯值吗?,python,pandas,dataframe,slice,Python,Pandas,Dataframe,Slice,我目前是一名Python自学初学者 for index, row in df_price.iterrows(): prod_weight = prod_weight = prod_data.loc[prod_data['sku'] == row['sku']]['weight'].item ------结果-------------------------------------- <bound method IndexOpsMixin.i

我目前是一名Python自学初学者

for index, row in df_price.iterrows():                    
    prod_weight = prod_weight = prod_data.loc[prod_data['sku'] == row['sku']]['weight'].item
------结果--------------------------------------

<bound method IndexOpsMixin.item of 18066    0.2
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18063    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18064    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of Series([], Name: weight, dtype: float64)>
<bound method IndexOpsMixin.item of 18062    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18058    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18059    0.1

---------------------------------------------------------
1.0
0.2
0.1
0.1
0.1
0.1
0.1
...
0.5
0.6
0.3
所以,我修改了代码如下

prod_weight = prod_data.loc[prod_data['sku'] == row['sku']]['weight'].item()
-------------结果--------------------------------

<bound method IndexOpsMixin.item of 18066    0.2
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18063    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18064    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of Series([], Name: weight, dtype: float64)>
<bound method IndexOpsMixin.item of 18062    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18058    0.1
Name: weight, dtype: float64>
<bound method IndexOpsMixin.item of 18059    0.1

---------------------------------------------------------
1.0
0.2
0.1
0.1
0.1
0.1
0.1
...
0.5
0.6
0.3
回溯(最近一次呼叫最后一次): 文件“D:/python\u project/price\u reviser/price\u reviser.py”,第58行,在 生产单位重量=生产单位数据。loc[生产单位数据['sku']==行['sku']]['weight']]。项() 文件“C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site packages\pandas\core\base.py”, 第719行,在项目中 返回self.values.item() ValueError:只能将大小为1的数组转换为Python标量

因此,我再次更改了代码,如下所示

prod_weight = prod_data.loc[prod_data['sku'] == row['sku']]['weight'].values

-------------result--------------------------------
[1.0]
[0.2]
[0.1]
[0.1]
[0.1]
[0.1]
[0.1]
...
[0.5]
[0.6]
[0.3]
prod_weight = re.sub('\[|''\]|''\'|', '', str(prod_data.loc[prod_data['sku'] == row['sku']]['weight'].values))
因此,我再次更改了代码,如下所示

prod_weight = prod_data.loc[prod_data['sku'] == row['sku']]['weight'].values

-------------result--------------------------------
[1.0]
[0.2]
[0.1]
[0.1]
[0.1]
[0.1]
[0.1]
...
[0.5]
[0.6]
[0.3]
prod_weight = re.sub('\[|''\]|''\'|', '', str(prod_data.loc[prod_data['sku'] == row['sku']]['weight'].values))
--------------结果-------

但是,

如何正确提取? 如何正确提取? 如何正确提取? 如何正确提取? 如何正确提取

for index, row in df_price.iterrows():
    try:        
        prod_weight= prod_data.loc[prod_data['sku'] == row['sku']]['weight'].item()
    except:
        prod_weight= prod_data.loc[prod_data['sku'] == row['sku']]['weight']
试试这个


错误
ValueError:只能将大小为1的数组转换为Python标量
,因为.item()仅适用于series/dataframe数据类型。如果您尝试执行任何scalar.item()操作,它将抛出此错误

能否显示数据帧(和列数据类型)的摘录以了解源数据的外观?在第一次尝试中,您只是列出未调用的函数(
),而不是执行它。在第二次尝试中,您遇到了有关Numpy阵列的Numpy新奇之处。您的第三次尝试似乎满足了您的需求(我猜,除了生成一个Numpy数组而不是一个float)。你最后的尝试。。。不要再这样做了,不要使用正则表达式(此处):。@susim您可以只打印df.head()(所有必要的列)吗?