我是Python的新手,还在学习熊猫。我希望我能得到一些帮助。
我做了什么
我要做什么
样本数据
这是合并数据
>>> rctf
CompName Tevent_id event_id
0 Server1 0.0 NaN
1 Server2 16.0 16.0
2 Server3 16.0 2.0
3 Server4 3.0 3.0
4 Server5 3.0 NaN
5 Server6 2.0 2.0
预期产量
CompName Tevent_id event_id status
0 Server1 0.0 NaN T Only
1 Server2 16.0 16.0 Match
2 Server3 16.0 2.0 No Match
3 Server4 3.0 3.0 Match
4 Server5 3.0 NaN T Only
5 Server6 2.0 2.0 Match
6 Server7 NaN 2.0 W Only
我尝试过的事情 我在下面找到了链接,这基本上是我想要做的,但是我在做多重条件运算。
https://chrisalbon.com/python/data_wrangling/pandas_create_column_using_conditional/
我一直在研究.where方法,但还没有真正弄清楚。
条件
答案 0 :(得分:0)
编写如下函数:
>>> for i in 1,2,3:
... print(i)
... else:
... print('here')
...
1
2
3
here
答案 1 :(得分:0)
您可以使用lambda:
import pandas as pd
import numpy as np
def isNaN(num):
return num != num
e = {
'CompName': ['Server1', 'Server2', 'Server3', 'Server4', 'Server5', 'Server6', 'Server7'],
'Tevent_id': [0.0, 16.0, 16.0, 3.0, 3.0, 2.0, np.nan],
'event_id': [ np.nan, 16.0, 2.0, 3.0, np.nan, 2.0, 2.0]
}
c_table = pd.DataFrame(data=e)
c_table['status'] = None
def process_row(row):
if (row['Tevent_id'] == row['event_id']):
return "Match"
elif (isNaN(row['Tevent_id']) and not isNaN(row['event_id'])):
return "W Only"
elif (not isNaN(row['Tevent_id']) and isNaN(row['event_id'])):
return "T Only"
elif (not isNaN(row['Tevent_id']) and not isNaN(row['event_id'])):
return "No Match"
else:
return "Both NaN"
c_table['status'] = c_table.apply(lambda row: process_row(row), axis=1)
print( c_table)
结果:
CompName Tevent_id event_id status
0 Server1 0.0 NaN T Only
1 Server2 16.0 16.0 Match
2 Server3 16.0 2.0 No Match
3 Server4 3.0 3.0 Match
4 Server5 3.0 NaN T Only
5 Server6 2.0 2.0 Match
6 Server7 NaN 2.0 W Only