您可以使用DataFrame.eval
:
import pandas as pd
df = pd.DataFrame({
"col_a":["Larry","Lee"],
"col_b":["Larry","Lee"],
"col_a1":["Peter","Jeremy"],
"col_b1":["Peter","Ilia"]
})
print df
df["flag"] = df.eval("col_a==col_b and col_a1==col_b1")
print df
輸出:
col_a col_a1 col_b col_b1
0 Larry Peter Larry Peter
1 Lee Jeremy Lee Ilia
col_a col_a1 col_b col_b1 flag
0 Larry Peter Larry Peter True
1 Lee Jeremy Lee Ilia False
如果碰巧要比較的列存儲在a_cols
和b_cols
兩個列表中,您可以做一些事情荷蘭國際集團這樣的:
a_cols = ["col_a","col_a1"]
b_cols = ["col_b","col_b1"]
df["flag"] = df.eval(" and ".join("%s==%s" % pair for pair in zip(a_cols,b_cols)))
print df
輸出:
col_a col_a1 col_b col_b1 flag
0 Larry Peter Larry Peter True
1 Lee Jeremy Lee Ilia False
它看起來像col_a,col_b是int df.eval的硬代碼,我們有一種方法可以按對循環列嗎?我們如何做到這一點? –
@LarryWoo你究竟想幹什麼?請給出更多細節,或者提出另一個問題,我會盡力幫助你;) –
例如,我們不清楚Dataframe中的列號,它是動態的,我會通過函數讀取列,獲取所有列和存儲在列表中,然後試圖通過對來比較數據,我如何循環List並使df.eval()工作? –