从CSV文件向Pandas Dataframe添加数据导致值错误

时间:2021-10-20 16:14:29

I am trying to add an int to an existing value in a Pandas DataFrame with

我正在尝试将一个int添加到Pandas DataFrame中的现有值

>>> df.ix['index 5','Total Dollars'] += 10

I get the error: ValueError: Must have equal len keys and value when setting with an iterable.

我得到错误:ValueError:使用iterable设置时必须具有相等的len键和值。

I think the error comes from the datatype as gotten from:

我认为错误来自于获取的数据类型:

>>> print type(df.ix['index 5','Total Dollars'] 
int64 <class 'pandas.core.series.Series'>

The dataframe is populated via CSV file. I tried loading the database from another CSV file:

数据框通过CSV文件填充。我尝试从另一个CSV文件加载数据库:

>>> print type(df.ix['index 5','Total Dollars']
int64

What could be causing this difference in type?

什么可能导致这种类型的差异?

1 个解决方案

#1


4  

This looks like a bug for some earlier pandas versions, fixed at least with 0.16.2 if not earlier as discussed here and here.

对于一些早期的熊猫版本来说,这看起来像一个bug,如果不是更早的话,至少修正了0.16.2,如本文和此处所述。

With 0.17.1, this works fine:

使用0.17.1,这很好用:

df = pd.DataFrame(data=[5], columns=['Total Dollars'], index=['index 5'])

         Total Dollars
index 5              5

df.ix['index 5', 'Total Dollars'] += 10

         Total Dollars
index 5             15

#1


4  

This looks like a bug for some earlier pandas versions, fixed at least with 0.16.2 if not earlier as discussed here and here.

对于一些早期的熊猫版本来说,这看起来像一个bug,如果不是更早的话,至少修正了0.16.2,如本文和此处所述。

With 0.17.1, this works fine:

使用0.17.1,这很好用:

df = pd.DataFrame(data=[5], columns=['Total Dollars'], index=['index 5'])

         Total Dollars
index 5              5

df.ix['index 5', 'Total Dollars'] += 10

         Total Dollars
index 5             15