Python 是否可以在Pandas中的loc中使用loc替换值?

Python 是否可以在Pandas中的loc中使用loc替换值?,python,pandas,replace,valueerror,loc,Python,Pandas,Replace,Valueerror,Loc,让我先画出我要解决的问题。我试图根据包含'-1'的行中的两个其他值,用同一列中的另一个值替换值'-1'。更清楚地说,这里有一个例子。在下面的数据框中,“所有者”列中缺少两个值。我想用“所有者”列中具有相同“价格”值的值替换每个“-1”值,该值是在“时间”中早于“-1”值出现的第一个值。因此,在本例中,第一个-1值位于第3行。相应的“价格”和“时间”是便宜的和2011-01-01 13:30:00。所以现在,我想用车主的名字来代替-1,车主有一辆便宜的车,这是在考虑中的那辆车之前的第一次,所以在2

让我先画出我要解决的问题。我试图根据包含'-1'的行中的两个其他值,用同一列中的另一个值替换值'-1'。更清楚地说,这里有一个例子。在下面的数据框中,“所有者”列中缺少两个值。我想用“所有者”列中具有相同“价格”值的值替换每个“-1”值,该值是在“时间”中早于“-1”值出现的第一个值。因此,在本例中,第一个
-1
值位于第3行。相应的“价格”和“时间”是
便宜的
2011-01-01 13:30:00
。所以现在,我想用车主的名字来代替
-1
,车主有一辆
便宜的
车,这是在考虑中的那辆车之前的第一次,所以在
2011-01-01 13:30:00之前的第一次。在本例中,这将是第1行中的一个,所有者名称为
Jane
。对于以下任何
-1
值(例如宝马),也应自动执行此操作

我想要解决这个问题的方法是,首先定位-1,然后保存相应的价格和时间,然后及时定位第一个相应的价格并替换所有者值。我希望通过以下方式使用Pandas Loc方法(我还包括了生成数据帧的代码)

将熊猫作为pd导入
从日期时间导入日期时间
汽车={‘品牌’:[‘本田’、‘丰田’、‘福特’、‘奥迪’、‘沃尔沃’、‘宝马’],
‘价格’:[‘便宜’、‘便宜’、‘很多’、‘便宜’、‘便宜’、‘很多’],
“时间”:[datetime.strTime('2008年1月1日下午1:30'、'%m/%d/%Y%I:%m%p')、datetime.strTime('2009年1月1日下午1:30'、'%m/%d/%Y%I:%m%p')、datetime.strTime('2010年1月1日下午1:30'、'%m/%d/%Y%I:%m%p')、datetime.strTime('2011年1月1日下午1:30'、'%m/%d/%Y%I:%m%p'),
datetime.strTime('2012年1月1日下午1:30','%m/%d/%Y%I:%m%p'),datetime.strTime('2013年1月1日下午1:30','%m/%d/%Y%I:%m%p'),
‘所有者’:[‘马克’、‘简’、‘菲尔’、‘1’、‘简’、‘1’]]
df=pd.DataFrame(汽车,列=['brand','price','time','owner']))
P_T=df.loc[df.owner='-1',['price','time']
df.loc[df.owner=='-1','owner']=df.loc[(df.price==P\T.price)和(df.time
正如您在最后一行中所看到的,这本质上是一个loc中的loc,而等式右侧的条件都基于p_T loc。然而,问题来了,因为我不断得到这个错误:
ValueError:只能比较标签相同的系列对象

我认为我做错了什么,可能做的事情没有尽可能有效率……所以我真的很想在这件事上得到一些帮助。

我认为你把这件事复杂化了-基本上你需要做的是用价格列分组的最后一个对应的结束值填充
-1
值?如果是这样的话,向前填充就可以了
ffill

import numpy as np
s = df.replace('-1',np.nan).sort_values('time').groupby(['price'])['owner'].ffill()

df['owner'] = df.index.map(s)



print(df)


    brand  price                time owner
0   Honda  cheap 2008-01-01 13:30:00  Marc
1  Toyota  cheap 2009-01-01 13:30:00  Jane
2    Ford   alot 2010-01-01 13:30:00  Phil
3    Audi  cheap 2011-01-01 13:30:00  Jane
4   Volvo  cheap 2012-01-01 13:30:00  Jane
5     Bmw   alot 2013-01-01 13:30:00  Phil

df和P_T的索引不一样,因此值_是错误的。你可以用``reindex``来做同样的事情。但我不确定你想要的是什么。我对熊猫没有那么多经验,所以我不知道ffill。非常感谢!@rikhendrick没问题,伙计,我稍微编辑了答案以保留你原来的索引
import pandas as pd
from datetime import datetime

cars = {'brand': ['Honda','Toyota','Ford','Audi','Volvo','Bmw'],
        'price': ['cheap','cheap','alot','cheap','cheap','alot'],
        'time': [datetime.strptime('1/1/2008 1:30 PM', '%m/%d/%Y %I:%M %p'),datetime.strptime('1/1/2009 1:30 PM', '%m/%d/%Y %I:%M %p'),datetime.strptime('1/1/2010 1:30 PM', '%m/%d/%Y %I:%M %p'),datetime.strptime('1/1/2011 1:30 PM', '%m/%d/%Y %I:%M %p'),
                 datetime.strptime('1/1/2012 1:30 PM', '%m/%d/%Y %I:%M %p'),datetime.strptime('1/1/2013 1:30 PM', '%m/%d/%Y %I:%M %p')],
        'owner': ['Marc', 'Jane','Phil','-1','Jane','-1']}

df = pd.DataFrame(cars, columns = ['brand', 'price','time','owner'])

P_T = df.loc[df.owner == '-1',['price','time']

df.loc[df.owner == '-1', 'owner'] = df.loc[(df.price == P_T.price)&(df.time < P_T.time), 'owner']
import numpy as np
s = df.replace('-1',np.nan).sort_values('time').groupby(['price'])['owner'].ffill()

df['owner'] = df.index.map(s)



print(df)


    brand  price                time owner
0   Honda  cheap 2008-01-01 13:30:00  Marc
1  Toyota  cheap 2009-01-01 13:30:00  Jane
2    Ford   alot 2010-01-01 13:30:00  Phil
3    Audi  cheap 2011-01-01 13:30:00  Jane
4   Volvo  cheap 2012-01-01 13:30:00  Jane
5     Bmw   alot 2013-01-01 13:30:00  Phil