[英]Pandas create new column in df2 from another df1 column2 if a value in df1 column1 matches value in a list
[英]In python: how to create new column with value of column and value of another between brackets. New column= column1(column2)
我有一個數據框,其中一列是計數,另一列是百分比。 我想在此數據框中創建新列以顯示計數(百分比)。
示例數據:
df=pd.DataFrame({'count': [1,2,3],
'percent': [30,35,35]})
print(df)
雖然我想要這樣的結果:
df=pd.DataFrame({'count': [1,2,3],
'percent': [30,35,35], 'count(percent)': ['1(30)', '2(35)', '3(35)']})
print(df)
您可以使用這一行:
df["count(percent)"] = df["count"].astype(str) +'('+df["percent"].astype(str)+')'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.