Python 熊猫-基于第三列从两个不同列中选择数据
我有3个数据帧/系列:Python 熊猫-基于第三列从两个不同列中选择数据,python,pandas,dataframe,conditional,Python,Pandas,Dataframe,Conditional,我有3个数据帧/系列: >>> max_q.head() Out[16]: 130 1.00 143 2.00 146 2.00 324 10.00 327 6.00 dtype: float64| >>> min_q.head() Out[17]: 130 8.00 143 6.00 146 4.00 324 8.00 327 8.00 dtype: float64 >>> dirx.
>>> max_q.head()
Out[16]:
130 1.00
143 2.00
146 2.00
324 10.00
327 6.00
dtype: float64|
>>> min_q.head()
Out[17]:
130 8.00
143 6.00
146 4.00
324 8.00
327 8.00
dtype: float64
>>> dirx.head()
Out[18]:
side
130 B
143 S
146 S
324 B
327 S
我想创建一个新的数据帧,当dirx='S'时它等于max_q,反之则等于min_q
有没有一个简单的方法可以做到这一点?到目前为止,以下方面都失败了:
1. np.where(dirx=='S', max_q, min_q)
2. rp = pd.DataFrame(max_q, columns = ['id'])
rp.loc[dirx=='B', 'id'] = min_q
你想保留索引吗?嗨,是的,这是最好的…我需要保留顺序。当添加
dirx['side']='B'
时,你的代码在这里工作。如果执行max_q.index.duplicated()
,会发生什么情况?第二个错误是什么method@RafaelC>>>max_q.index.duplicated()Out[79]:数组([False,False,False,…,False,False,False],dtype=bool)