![](/img/trans.png)
[英]How to convert a DataFrame that only has unique value in one column based on one column in specific value in Pandas
[英]Extracting the value from a Pandas Dataframe column that has only a unique value
給定一個熊貓DataFrame(df),其中一列(unique_val_col)應該具有唯一值,那么提取此值(而不是列表)的最佳方法是什么?
到目前為止,我已經使用以下代碼:
output = list(set(df[unique_val_col)))
if len(output)==1: output = output[0]
或者,如果有機會使用nan,則將第一行更改為:
output = [val for val in list(set(df[unique_val_col))) if val == val]
問題是是否存在更直接的方法,這也反映出以下事實:該列實際上僅具有一個值,而無需使用“ if”語句。
我認為您正在嘗試查找僅出現一次的值,如果那樣的話,您可以像這樣實現它
df['unique_value_counts'].value_counts().sort_values(ascending=False).keys()[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.