我如何:
两个独特项目清单:
twentysix_above = '_26+' (value is equal or greater than 26)
six_to_twentyfive = '_25' (value is between 6 and 25)
one_to_five = '_5' (value is between 1 and 5)
如果item在list1中,请将以下变量值附加到项目的末尾,并将其附加到“new_item”列:
twentyone_above = '_21+' (value is equal or greater than 21)
one_to_twenty = '_20' (value is between 1 and 20)
如果item在list2中,请将以下变量值附加到每个项目的末尾,并将其附加到“new_item”列:
>> print df
item number
0 one 4
1 door 55
2 sun 2
3 tires 62
4 tires 7
5 water 94
>> list1 = ['one','two','shoes']
>> list2 = ['door','four','tires']
>> df['match'] = df.item.isin(list1)
>> bucket = []
>> for row in df.itertuples():
if row.match == True and row.item > 25:
bucket.append(row.item + '_26+')
elif row.match == True and row.item >5:
bucket.append(row.item + '_25')
elif row.match == True and row.item >0:
bucket.append(row.item +'_5')
else:
bucket.append(row.item)
df['new_item'] = bucket
>> print df
item number match new_item
0 one 4 True one_5
1 door 55 True door
2 sun 2 False sun
3 tires 62 True tires
4 tires 7 True tires
5 water 94 False water
如果该项目不在任一列表中,请将项目名称移至“new_item”列。
Dataframe列将包含其中每个列表中的一个,一些或没有“items”以及“number”列中的关联数字。我已经部分到了,但我不确定如何与其他列表进行比较并将其全部放入'new_item'列?任何帮助表示赞赏,谢谢!
item number new_item
0 one 4 one_20
1 door 55 door__21+
2 sun 2 sun
3 tires 62 tires_21
4 tires 7 tires_20
5 water 94 water
所需结果:(比较两个列表,可能不需要布尔检查列)
accountDelete(id, name) {
let accountsToAdd = this.userForm.value.accountsToAdd;
const accountsToDelete = this.userForm.value.accountsToDelete;
accountsToDelete.push(
{
id: id,
name: name
}
);
console.log(accountsToDelete);
accountsToAdd = accountsToAdd.filter(account => account.id !== id);
console.log(accountsToAdd);
}
答案 0 :(得分:1)
看起来你想要的结果有点偏。第一行在列表一中,其值为4,因此它应该是' one_5'正确?
无论如何,这可以通过布尔掩码实现。 DataFrames有一个有用的isin()函数,可以很容易地找到值是否在列表中。那么你还有两个条件,如果你需要两个数字之间的值,或者如果范围是无界的则只需要一个条件。
import pandas as pd
import numpy as np
df = pd.DataFrame({'item': ['one', 'door', 'sun', 'tires', 'tires', 'water'],
'number': [4, 55, 2, 62, 7, 94]})
list1 = ['one','two','shoes']
list2 = ['door','four','tires']
df['new_item'] = df['item']
logic1 = np.logical_and(df.item.isin(list1), df.number > 25)
logic2 = np.logical_and.reduce([df.item.isin(list1), df.number > 5, df.number <= 25])
logic3 = np.logical_and.reduce([df.item.isin(list1), df.number > 1, df.number <= 5])
logic4 = np.logical_and(df.item.isin(list2), df.number >= 21)
logic5 = np.logical_and.reduce([df.item.isin(list2), df.number > 1, df.number < 20])
df.loc[logic1,'new_item'] = df.loc[logic1,'item']+'_26+'
df.loc[logic2,'new_item'] = df.loc[logic2,'item']+'_25'
df.loc[logic3,'new_item'] = df.loc[logic3,'item']+'_5'
df.loc[logic4,'new_item'] = df.loc[logic4,'item']+'_21+'
df.loc[logic5,'new_item'] = df.loc[logic5,'item']+'_20'
我们将此作为输出