Issue
Is there a way to get percentage & roundoff the list of values in a df column. I have a df as
id name value
============================================
1 AA [0.0012, 0.0022, 0.365]
2 BB [0.0412, 0.822, 0.3]
3 CC [0.692, 0.0002, 0.56]
Is there a way to the above df to
id name value
============================================
1 AA [0.1, 0.2, 36.5]
2 BB [4.1, 82.2, 3]
3 CC [69.2, 0.0, 56]
Thanks,
Solution
You can try like this
import pandas as pd
data = {
'id': [1, 2, 3],
'name': ['AA', 'BB', 'CC'],
'value': [
[0.0012, 0.0022, 0.365],
[0.0412, 0.822, 0.3],
[0.692, 0.0002, 0.56]
]
}
df = pd.DataFrame(data)
def convert_to_percentage(value_list):
return [round(val * 100, 1) for val in value_list]
df['value'] = df['value'].apply(convert_to_percentage)
print(df)
Output:
id name value
0 1 AA [0.1, 0.2, 36.5]
1 2 BB [4.1, 82.2, 30.0]
2 3 CC [69.2, 0.0, 56.0]
Answered By - Mahboob Nur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.