![](/img/trans.png)
[英]How to propagate values from one row in a pandas Dataframe to all other rows
[英]How to propagate values in one column to rows in other columns (pandas dataframe)
我有這種格式的原始數據(輸入實際上是一個巨大的 excel 表),由於一列中的唯一值生成了多行,有點像這個例子:
data = [['tom', 10, 'vanilla'], ['tom', 10, 'chocolate'], ['nick', 15, 'strawberry'], ['nick', 15, 'butter pecan'], ['nick', 15, 'mint chocolate chip'], ['juli', 14, 'strawberry'], ['juli', 14, 'chocolate'], ['juli', 14, 'vanilla']]
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Favorite Ice Cream'])
但我需要從上面的原始數據以下面的格式創建一個數據框,其中最后一列被擴展為行並消除重復的行:
data2 = [['tom', 10, 'vanilla', 'chocolate', '', ''], ['nick', 15, 'butter pecan', '', 'strawberry', 'mint chocolate chip'], ['juli', 14, 'vanilla', 'chocolate', 'strawberry', '']]
df2 = pd.DataFrame(data, columns = ['Name', 'Age', 'Favorite Vanilla-based Ice Cream', 'Favorite Chocolate-based Ice Cream', 'Favorite Berry Ice Cream', 'Favorite Other Ice Cream'])
感謝您的幫助!
使用pivot
和映射字典:
mapping = {'Vanilla': ['vanilla', 'butter pecan'],
'Chocolate': ['chocolate'],
'Berry': ['strawberry']}
reverse_mapping = {v: k for k, l in mapping.items() for v in l}
df['X-based'] = df['Favorite Ice Cream'].map(reverse_mapping).fillna('Other')
out = df.pivot(index=['Name', 'Age'],
columns='X-based',
values='Favorite Ice Cream') \
.rename_axis(columns=None).reset_index().fillna('')
輸出:
>>> out
Name Age Berry Chocolate Other Vanilla
0 juli 14 strawberry chocolate vanilla
1 nick 15 strawberry mint chocolate chip butter pecan
2 tom 10 chocolate vanilla
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.