【问题标题】:TypeError: cannot convert the series to <class 'float'> iterrowsTypeError:无法将系列转换为 <class 'float'> iterrows
【发布时间】:2018-08-12 13:07:07
【问题描述】:

我试图让 python 从 csv 读取一行,然后将内容放入 DynamoDB,然后对每个后续行执行相同操作,直到结束。我修改了亚马逊的示例代码,使其如下所示:

from __future__ import print_function # Python 2/3 compatibility
import boto3
import decimal
import pandas as pd

dynamodb = boto3.resource('dynamodb', region_name='us-west-2', aws_access_key_id='123456789', aws_secret_access_key='987654321')

table = dynamodb.Table('Loyalty_One')

data = pd.read_csv('testdb.csv')
data[['collector_key']] = data[['collector_key']].astype(float)

for i, rows in data.iterrows():
    collector_key = float(data['collector_key'])
    sales = float(data['sales'])

    print("Adding data:", collector_key, sales)

    table.put_item(
        Item={
            'collector_key': collector_key,
            'sales': sales,
           }
       )

当我这样做时,我收到错误:TypeError: cannot convert the series to

你可以在这里看到文件,这没什么特别的: https://s3.amazonaws.com/vshideler-data/testdb.csv

在我看来,第 16 行(和第 17 行)应该提取列中每一行的内容。我认为标头可能会产生干扰,但是使用“header=None”执行 pd.read_csv 只会让我遇到一个完全不同的错误。

(虽然我想到我可能只使用原始数据框而不是 csv 文件)

【问题讨论】:

  • 在您的问题中提供Minimal, Complete, Verifiable Example
  • 这没什么用。我的帖子有什么问题?
  • 您是否尝试为此数据更改此 float(data['collector_key'])[['collector_key']].astype(float)
  • 这行得通(谢谢),但我在第 17 行遇到了同样的错误。我尝试将格式更改为“sales = (data[['sales']])”,但后来我得到了错误:“TypeError:值“collector_key”的不支持类型“”尝试“sales = data[['sales']].astype(float)”得到错误:TypeError:价值“销售”的不支持类型“

标签: python pandas dataframe amazon-dynamodb boto3


【解决方案1】:

我希望这会有所帮助。

如果项目的类型是系列并且只有一个数字。您可以使用传统的类型转换来转换类型。

例如,您可以这样投射...

XX=AA[AA.SHOUT="HAHA"]["TYPE"] # Get the Type column and find value...
XX=float(XX) if len(XX)==1 else np.NaN
print(XX)

在真正严肃的代码中,您必须检查更多的异常,但对于有限的场景,这将是转换数据的最简单方法。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2017-10-16
    • 1970-01-01
    相关资源
    最近更新 更多