[英]Find maximum value of a column and return the corresponding row values using Pandas
[英]Compare single value to column of values and find maximum in pandas
我有一個值,我試圖將它與一列進行比較並找到每行之間的最大值,如下所示:
input:
val = 5
df['col1'] = [4,6,2]
expected output:
df['col1'] = [5,6,5]
到目前為止,我采用的方法是嘗試使用 max 函數找到最大值:
df['col1'] = max(val, df['col1'])
The error I'm getting is as follows: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我猜這是因為它無法將列中的一個值與多個值進行比較,但我不確定如何解決這個問題。
您可以使用列表理解:
df['col1'] = [max(val,el) for el in df['col1']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.