如何在資料框中創建一個新列,如果價格低于 50000,則顯示“便宜”,“公平”是價格在 50000 和 100000 之間,如果價格超過 100000,則顯示“昂貴”在此處輸入影像描述
uj5u.com熱心網友回復:
雖然我認為@mozway 的解決方案是最干凈的,但這是另一種使用方式numpy.select
import numpy as np
df['new_column'] = np.select([df['selling_price'] < 50_000,
df['selling_price'] <= 100_000],
['Cheap', 'Fair'], 'Expensive')
uj5u.com熱心網友回復:
有很多選擇。一個不錯的是pandas.cut:
df['new'] = pd.cut(df['selling_price'],
bins=[0,50000,100000, float('inf')],
labels=['cheap', 'fair', 'expensive'])
uj5u.com熱心網友回復:
您可以使用 numpy.where() 進行這種資料處理:
import numpy as np
df['Cheap']=np.where(df['selling_price']<=50000,'Cheap', #When selling_price <50k, 'Cheap', otherwise...
np.where((df['selling_price']>50000) & (df['selling_price']<100000) ,'Fair', #When selling_price >50k and <100k, 'Fair', otherwise...
np.where(df['selling_price']>=100000,'Expensive',#When selling_price >100k, Expensive
'N/A')))#Otherwise N/A - in case you have some string or other data type in your data
uj5u.com熱心網友回復:
apply()和功能的另一種方式lambda:
df["new"] = df.selling_price.apply(
lambda x: "cheap" if x < 50000 else
"fair" if x < 100000 else
"expensive"
)
或者以允許您在條件中包含多個列的一般方式:
df["new"] = df.apply(
lambda x: "cheap"
if x["selling_price"] < 50000
else "fair"
if x["selling_price"] < 100000
else "expensive",
axis=1,
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450700.html
標籤:python-3.x 熊猫 数据框 数据科学
