![](/img/trans.png)
[英]Python - how to create new columns in a dataframe from the unique values from an existing column with corresponding values?
[英]Create a new column in a dataframe consisting of values from existing columns
我有一個看起來像這樣的數據框:
X Y Corr_Value
0 51182 51389 1.00
1 51182 50014 NaN
2 51182 50001 0.85
3 51182 50014 NaN
我想創建一個由X
和Y
列的值組成的新列。 想法是遍歷行,如果Corr_Value
不為null,則新列應顯示:
Solving (X column value) will solve (Y column value) at (Corr_value column)% probability.
例如,對於第一行,結果應為:
Solving 51182 will solve 51389 with 100% probability.
這是我寫的代碼:
dfs = []
for i in df1.iterrows():
if ([df1['Corr_Value']] != np.nan):
a = df1['X']
b = df1['Y']
c = df1['Corr_Value']*100
df1['Remarks'] = (f'Solving {a} will solve {b} at {c}% probability')
dfs.append(df1)
df1
是存儲X
, Y
和Corr_Value
數據的數據幀。
但是似乎有一個問題,因為我得到的結果看起來像這樣:
但是結果應如下所示:
如果您可以幫助我獲得理想的結果,那就太好了。
使用DataFrame.dropna
用於刪除丟失的行和應用f-string
S表示自定義輸出字符串DataFrame.apply
:
f = lambda x: f'Solving {int(x["X"])} will solve {int(x["Y"])} at {int(x["Corr_Value"] * 100)}% probability.'
df['Remarks'] = df.dropna(subset=['Corr_Value']).apply(f,axis=1)
print (df)
X Y Corr_Value Remarks
0 51182 51389 1.00 Solving 51182 will solve 51389 at 100% probabi...
1 51182 50014 NaN NaN
2 51182 50001 0.85 Solving 51182 will solve 50001 at 85% probabil...
3 51182 50014 NaN NaN
您還可以在以下位置使用numpy:
import numpy as np
df['Remarks'] = np.where(df.Corr_Value.notnull(), 'Solving ' + df['X'].astype(str) + ' will solve ' + df['Y'].astype(str) + ' with ' + (df['Corr_Value'] * 100).astype(str) + '% probability', df['Corr_Value'])
輸出:
X Y Corr_Value Remarks
0 51182 51389 1.00 Solving 51182 will solve 51389 with 100.0% pro...
1 51182 50014 NaN NaN
2 51182 50001 0.85 Solving 51182 will solve 50001 with 85.0% prob...
3 51182 50014 NaN NaN
你試一試:
dfs = []
for i, r in df1.iterrows():
if (r['Corr_Value'] != np.nan):
a = r['X']
b = r['Y']
c = r['Corr_Value']*100
df1.at[i, 'Remarks'] = "Solving "+ str(a) + " will solve " + str(b) + " at " + str(c) + " % probability"
我認為問題與使用df1
而不是當前行有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.