python-3.x 我们如何在一列中搜索几个字符串,并将另一列乘以一个常数?

o7jaxewo  于 2022-12-24  发布在  Python
关注(0)|答案(1)|浏览(128)

我想我可以在一列中搜索一个字符串,如果找到了结果,将另一列中的值乘以一个字符串,就像这样。

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返回的!

jhkqcmku

jhkqcmku1#

我们可以尝试一种不同的方法:

pats = {'03 Tree':15,
        '17 Tree':15,
        '26 Vines':5,
        'overgrown primary':12,
        'Tree fails':10}

df['MaintCost'] = df.apply(lambda x: x.Expense * pats.get(x.Building,0), axis=1)
print(df)
'''
        Month  Expense  Revenue           Building  MaintCost
0  2020-01-01     1000   -50000            03 Tree      15000
1  2020-02-01     3000    40000            17 Tree      45000
2  2020-03-01     7000    50000         Tree fails      70000
3  2020-04-01     3000    40000  overgrown primary      36000
4  2020-01-01     5000    -6000         Tree fails      50000
5  2020-02-01     5000     4000           26 Vines      25000
6  2020-03-01     5000     9000           26 Vines      25000
7  2020-04-01     6000    10000         Tree fails      60000

相关问题