我正在尝试基于条件表达式覆盖DataFrame中的新列:
将df‘
if df['service'] == 'PE1' or 'PE2':
# if ['load port] == 'ABC' then new value == PE1ABC
# in other words != 'PE1' or 'PE2'.
中保留原始值
我正在尝试从另一个DataFrame使用.merge()来"VLOOKUP“。但是,'PE1‘和'PE2’服务需要加载端口。所有其他服务都有1:1的分配。
发布于 2018-07-24 08:24:45
您可以根据自己的条件定义一个函数,然后使用apply
函数更改列。
示例数据帧:
import pandas as pd
df = pd.DataFrame({'service':['PE1','PE2','bla','ble','PE2'],\
'load port':['ABC','TEST','BLA','BLA','BLE']})
输出:
load port service
0 ABC PE1
1 TEST PE2
2 BLA bla
3 BLA ble
4 BLE PE2
更改函数:
def changeService(row):
if row['service'] == 'PE1' or row['service'] == 'PE2':
return row['service'] + row['load port']
return row['service']
应用更改函数以覆盖您的列:
df['service'] = df.apply(changeService, axis = 1)
输出:
load port service
0 ABC PE1ABC
1 TEST PE2TEST
2 BLA bla
3 BLA ble
4 BLE PE2BLE
注意:建议您的更改函数始终具有return
,否则某些行将被NaN
值填充。
发布于 2018-07-24 10:07:22
您可以使用numpy.where执行以下任务:
import numpy as np
df['service'] = np.where((df['service'] =='PE1')|(df['service'] =='PE2'), #conditions
df['service']+df['load port'], #result if conditions are met
df['service']) # result if not
来自@Lorran Sutter的apply
方法很好,但是如果你的数据帧很大,这个方法会更快。
https://stackoverflow.com/questions/51488612
复制相似问题