Count in python

Dear all,

I am a new joiner in the python community.

In the following python I am simply counting the number of company in an excel…but I would like to show only the companies with more than 1 count.

Current python:
`

import pandas as pd

data = pd.read_excel (r'/Users//Documents/Test_python.xlsx', sheet_name='Feuil1')
df = pd.DataFrame(data, columns= ['Company','volume'])

dt = df['Company'].value_counts()
print(dt)

`
Current result:

Swiss Re 63
MS 63
Allianz 53
Liverpool 48
Mancherster 15
Conoco 4
Zurich 3
HSBC 1
Goldman Sachs 1
Name: Company, dtype: int64

Expected result:

Swiss Re 63
MS 63
Allianz 53
Liverpool 48
Mancherster 15
Conoco 4
Zurich 3
Name: Company, dtype: int64

Thank you for your help!

Best

T

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

First, you will need to find out what type of value gets returned when you call:

df['Company'].value_counts()

That is, is it a string array, or a number?
If it is a string array, you will need to perform a type conversion on it, so that it becomes a number.

From there, you can perform something like this:

dt = df['Company'].value_counts()

for company in dt:
  if company.value() > 1:
    print(company)

Hope this helps. Without any extra information, I cannot tell you any more.

“df.iloc” method to be used for selecting columns and
find your row then with loop or whatever
Hope this helps.

Hi Sky020,

Thank you for your answer, much appreciated!

I realise the count on a string array as per the attached:

Overall that’s what I would like to realised:

  1. Count the companies (step done)
  2. Only return the companies with more than one count
  3. For each of the company list in which “topic” (column B of the excel) they are present
  4. Send everything by mail on a weekly basis (step done)

I struggle on the steps 2 and 3.

Thank you again for your help

Best

T

Thank you!
Duly noted for the next post.

Best

T