基于标签的索引Pandas(.loc)

2024-04-26 05:13:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近意识到了链式赋值的危险性,我正在尝试使用loc[rowindex,colindex]在熊猫中使用正确的索引方法。我正在处理混合数据类型(在np.float64和list和string的同一系列中混合)-这是不可避免的。我有一个整数索引

我正在通过一个数据帧运行以下循环

Count = 0
for row in DF.index:
print row
    if '/' in str(DF.order_no[row]) and '/' not in str(DF.buyer[row]) and '/' not in    str(DF.buyer[row])\
    and '/' not in str(DF.smv[row]) and '/' not in str(DF.item[row]):
        DF.loc[row, 'order_no'] = str(DF.loc[row, 'order_no']).split('/')
        Count +=1

计数

返回错误:

 TypeError: object of type 'int' has no len()

我做错什么了?

在这个循环中,我可以做到:

print DF.loc[row, 'order_no']

以及

print DF.loc[row, 'order_no'] == str(DF.loc[row, order_no]).split('/')

但不是

DF.loc[row, 'order_no'] = str(DF.loc[row, order_no]).split('/')

使用print语句,我看到它卡在第3行,但是:

DF.loc[3, 'order_no']

工作得很好。

帮助通知。

编辑

解决方法如下:

Count = 0
Vals = []
Ind = []
for row in DF.index:
    if '/' in str(DF.order_no[row]) and '/' not in str(DF.buyer[row]) and '/' not in str(DF.buyer[row])\
    and '/' not in str(DF.smv[row]) and '/' not in str(DF.item[row]):
        Vals.append(DF.order_no[row].split('/'))
        Ind.append(row)
        Count +=1

DF.loc[Ind, 'order_no'] = Vals    

换句话说,我可以创建一个要修改的值的列表,然后使用.loc更改它们。这很好,这让我相信问题不在于我要分配的值,也不在于分配过程本身。

下面是我正在处理的数据类型的一个示例:据我所知,代码在第3行和第9行失败。抱歉,它的csv格式,但这是我如何阅读成熊猫。

https://www.dropbox.com/s/zuy8pj15nlhmcfb/EG2.csv

如果完成以下操作,则使用该数据:

EG = pd.reas_csv('EG.csv')
EG.loc[3, 'order_no'] = str(EG.loc[3, 'order_no']).split('/')

因错误而失败

object of type 'int' has no len()

但是

EG['order_no'][3] = str(EG.loc[3, 'order_no']).split('/')

工作很好,但这是我试图避免的链分配类型,因为它给了我其他地方的问题。

这就是为什么我认为这只是一个语法错误。

很抱歉现在有个不舒服的问题


Tags: andcsvnoindfcountnotorder
2条回答

您可能遇到了数据类型问题。以下代码对我有效:

import pandas as pd
data = {'working_hr': {3: 9.0}, 'order_no': {3: 731231}}
df = pd.DataFrame.from_dict(data, dtype=object)

然后:

>>> df.loc[3, 'order_no'] = [1, 2]
>>> df
  order_no working_hr
3   [1, 2]          9

注意dtype=object。这可能是缩短数据帧时错误消失的原因,特别是在从csv读取数据时。在许多情况下(例如从CSV读取),pandas尝试推断数据类型并选择最具体的类型。如果dtype是object,则可以将列表指定为值,但如果它是float64(例如)。因此,请检查您的混合类型列是否真的设置为dtypeobject

同样适用于您提供的CSV:

>>> df = pandas.read_clipboard(sep='\t', index_col=0)
>>> df
        buyer          order_no                                 item         smv
0         H&M            992754                        Cole tank top        6.17
1         H&M            859901                         Thilo Bottom        8.55
2         H&M            731231               Palma Short Sleeve Tee        5.65
3         H&M     731231/339260                      Palma Price Tee        5.65
4         H&M     859901/304141  Thilo Paijama Set top/Elva Tank Top   5.80/5.58
5         H&M            768380                       Folke Tank Top           6
6         H&M     596701/590691                        Paul Rock Tee        7.65
7    H&M/Mexx  731231/KIEZ-P002        Palma Short Sleeve Tee/Shorts  5.65/12.85
8         NaN               NaN                                  NaN         NaN
9  Ginatricot     512008/512009                           J.Tank top         4.6
>>> df.loc[3, 'order_no'] = str(df.loc[3, 'order_no']).split('/')
>>> df
        buyer          order_no                                 item         smv
0         H&M            992754                        Cole tank top        6.17
1         H&M            859901                         Thilo Bottom        8.55
2         H&M            731231               Palma Short Sleeve Tee        5.65
3         H&M  [731231, 339260]                      Palma Price Tee        5.65
4         H&M     859901/304141  Thilo Paijama Set top/Elva Tank Top   5.80/5.58
5         H&M            768380                       Folke Tank Top           6
6         H&M     596701/590691                        Paul Rock Tee        7.65
7    H&M/Mexx  731231/KIEZ-P002        Palma Short Sleeve Tee/Shorts  5.65/12.85
8         NaN               NaN                                  NaN         NaN
9  Ginatricot     512008/512009                           J.Tank top         4.6

较短的错误提示代码供参考(直到OP将其包含在问题中):

import pandas as pd
data = {'working_hr': {3: 9.0}, 'order_no': {3: 731231}}
df = pd.DataFrame.from_dict(data)
df.loc[3, 'order_no'] = [1,2] # raises error

在检查代码时,列表值[1,2]由带有列表索引器的setitem处理,对于作为标量处理的值,我看不出如何避免这个问题。

相关问题 更多 >

    热门问题