Observatory Gromme
Observatory Gromme

Reputation: 25

Getting a percent for one value in a column list

I'm looking for a solution to give me the percent of only one value (nr '1') in a column list. The list contains 3 numbers: 1,2 and 3. 7200 rows.

The code below gives me back the full numbers

Clouds = df['CloudC'].value_counts(normalize=True).round(decimals=4)*100

3    55.60
1    32.99
2    11.42
Name: CloudC, dtype: float64

If possible in one code: The 3 numbers in percent in this order: 1,2,3 including ',' in between

example: 32.99,11.42,55.60

If not possible, then the code for each value (1,2,3) seperated. But i only need the percent, not the name of the value as output.

example: 32.99 (from value 1)

Thanks.

UPDATE: The 3 options worked very well. How can i let the command line always look for number 1, 2 and 3. I forget this to mention because i was working with a document that for the moment all showed 1, 2, and 3 in the column. But i want the program to look for 1,2,3 all the time, and if a value is not founded, it displays for instance: 0.00, 0.00, 100. Any thoughts?

Upvotes: 2

Views: 66

Answers (1)

jezrael
jezrael

Reputation: 862761

Use Series.sort_index with convert values to strings by Series.astype and then use join:

print (', '.join(Clouds.sort_index().astype(str)))
32.99, 11.42, 55.6

Another solution, thanks @Vishnudev:

print (Clouds.sort_index().astype(str).str.cat(sep=', '))
32.99, 11.42, 55.6

If need list:

print (Clouds.sort_index().tolist())
[32.99, 11.42, 55.6]

Upvotes: 2

Related Questions