从 Pandas 中的两列中删除匹配值
Remove matching values from two columns in Pandas
我从等式
生成了以下数据集
df.loc[:,51] = [1217.0, -20.0, 13970.0, -74]
我删除了负值(特定值)并得到了这个
df.loc[:,52] = [1217.0, 0, 13970.0, 0]
现在我正在尝试使用删除的值获取另一列
df.loc[:,53] = df.drop_duplicates(subset=[df.loc[:,51], df.loc[:,52]])
我想要这个结果。
丢弃的值
df.loc[:,53] = [0,-20, 0,-74]
但是我得到以下错误
TypeError: 'Series' 对象是可变的,因此它们不能被散列
df.loc[:,53] = np.where(df.loc[:,51] == df.loc[:,52], 0, df.loc[:,51])
在这里,我已经用示例数据完成了:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
'rating': [4, -4, 3.5, -15, 5]
})
df.loc[(df['rating'] < 0 ), 'new_col'] = 0
df.loc[(df['rating'] > 0 ), 'new_col'] = df['rating']
df['dropped'] = np.where(df['rating'] == df['new_col'], 0, df['rating'])
df
我从等式
生成了以下数据集df.loc[:,51] = [1217.0, -20.0, 13970.0, -74]
我删除了负值(特定值)并得到了这个
df.loc[:,52] = [1217.0, 0, 13970.0, 0]
现在我正在尝试使用删除的值获取另一列
df.loc[:,53] = df.drop_duplicates(subset=[df.loc[:,51], df.loc[:,52]])
我想要这个结果。 丢弃的值
df.loc[:,53] = [0,-20, 0,-74]
但是我得到以下错误
TypeError: 'Series' 对象是可变的,因此它们不能被散列
df.loc[:,53] = np.where(df.loc[:,51] == df.loc[:,52], 0, df.loc[:,51])
在这里,我已经用示例数据完成了:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
'rating': [4, -4, 3.5, -15, 5]
})
df.loc[(df['rating'] < 0 ), 'new_col'] = 0
df.loc[(df['rating'] > 0 ), 'new_col'] = df['rating']
df['dropped'] = np.where(df['rating'] == df['new_col'], 0, df['rating'])
df