我想我可以在一列中搜索一个字符串,如果找到了结果,将另一列中的值乘以一个字符串,就像这样。
df_merged['MaintCost'] = df_merged.loc[df_merged['Code_Description'].str.contains('03 Tree','17 Tree'), 'AvgTotal_OH_Miles'] * 15
df_merged['MaintCost'] = df_merged.loc[df_merged['Code_Description'].str.contains('26 Vines'), 'AvgTotal_OH_Miles'] * 5
df_merged['MaintCost'] = df_merged.loc[df_merged['Code_Description'].str.contains('overgrown primary', 'Tree fails'), 'AvgTotal_OH_Miles'] * 12
这可能不起作用,因为我在名为'Code_Description'的列中有一个类似于'03 Tree'的字符串,而在'MaintCost'中我有NAN。
这里有一个例子来说明这一点,我使用的 Dataframe 和列名略有不同。
data = [{'Month': '2020-01-01', 'Expense':1000, 'Revenue':-50000, 'Building':'03 Tree'},
{'Month': '2020-02-01', 'Expense':3000, 'Revenue':40000, 'Building':'17 Tree'},
{'Month': '2020-03-01', 'Expense':7000, 'Revenue':50000, 'Building':'Tree fails'},
{'Month': '2020-04-01', 'Expense':3000, 'Revenue':40000, 'Building':'overgrown primary'},
{'Month': '2020-01-01', 'Expense':5000, 'Revenue':-6000, 'Building':'Tree fails'},
{'Month': '2020-02-01', 'Expense':5000, 'Revenue':4000, 'Building':'26 Vines'},
{'Month': '2020-03-01', 'Expense':5000, 'Revenue':9000, 'Building':'26 Vines'},
{'Month': '2020-04-01', 'Expense':6000, 'Revenue':10000, 'Building':'Tree fails'}]
df = pd.DataFrame(data)
df
df['MaintCost'] = df.loc[df['Building'].str.contains('03 Tree','17 Tree'), 'Expense'] * 15
df['MaintCost'] = df.loc[df['Building'].str.contains('26 Vines'), 'Expense'] * 5
df['MaintCost'] = df.loc[df['Building'].str.contains('overgrown primary', 'Tree fails'), 'Expense'] * 12
df['MaintCost'] = df.loc[df['Building'].str.contains('Tree fails'), 'Expense'] * 10
df['MaintCost'] = df['MaintCost'].fillna(100)
df
结果:
首先,我希望在第0行看到15000,但我得到了100,因为第0行是作为一个NAN返回的!
1条答案
按热度按时间jhkqcmku1#
我们可以尝试一种不同的方法: