![](/img/trans.png)
[英]how to create new columns in pandas based on a column's values within a dataframe (pivot or unpivot ? )
[英]How can I create a new column in a pandas pivot table with only matching values of populated columns?
我有一個pandas數據透視表,列出了列中的行和數據源中的個人。 有數百人在這些行中徘徊,數百個來源沿着列發生。
Desired_Value Source_1 Source_2 Source_3 ... Source_50
person1 20 20 20 20
person2 5 5 5 5
person3 Review 3 4 4 4
...
person50 1 1 1
我想要做的是創建上面的Desired_Value列。 只要它匹配所有值(忽略空白字段),我想引入一個值。 如果值不匹配,我想顯示Review。
我使用這個pandas命令將我的df打印到excel當前(沒有任何Desired_Value列):
df13 = df12.pivot_table(index='person', columns = 'source_name', values = 'actual_data', aggfunc='first')
我是Python的新手,如果這是一個愚蠢的問題,請道歉。
這是一種方法:
df = df13.copy()
df = df.astype('Int64') # So NaN and Int values can coexist
# Create new column at the front of the data frame
df['Desired_Value'] = np.nan
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1]
df = df[cols]
# Loop over all rows and flag columns for review
for idx, row in df.iterrows():
val = row.dropna().unique()
if len(val) == 1:
df.loc[idx, 'Desired_Value'] = val
else:
df.loc[idx, 'Desired_Value'] = 'Review'
print(df)
Desired_Value Source_1 Source_2 Source_3 Source_50
person1 20 20 20 NaN 20
person2 5 5 NaN 5 5
person3 Review 3 4 4 4
person50 1 1 NaN NaN 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.