简体   繁体   中英

In python: how to create new column with value of column and value of another between brackets. New column= column1(column2)

I have a data frame with counts in a column and percentages in another. I want to create new column in this data frame presenting counts(percentages).

example data:

df=pd.DataFrame({'count': [1,2,3],
    'percent': [30,35,35]})
print(df)

while I want the result like this:

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)+')'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM