Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count in python
#1
Dear all,

I am a new joiner in the Python adventure.

I would like to count the data in the column 'Company' and know in which of the 'Selection' they are and then send the result by mail.

I realised the first and the last step but struggle a bit to get all the corresponding 'Selection' for a specific company.

Extract of the excel:
Company Open Selection
Allianz 26151.660156 Topic 2
Zurich 26076.210938 Topic 3
Allianz 26287.839844 Topic 2
Conoco 26519.390625 Topic 5
Conoco 26726.250000 Topic 6
Allianz 26705.250000 Topic 7
Zurich 26601.580078 Topic 8
Allianz 26536.859375 Topic 2
Conoco 26418.269531 Topic 3
Conoco 26407.660156 Topic 4


Current Python:

import pandas as pd
import smtplib
from email.mime.text import MIMEText


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)

gmail_user = "XXXX"
gmail_appPassword = "XXXXX"

sent_from = ['XXXX']
to = ['XXXXX']

text = dt

msg = MIMEText(text)

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_user, gmail_appPassword)
server.sendmail(sent_from, to, msg.as_string())
server.quit()
Current result...and the idea would be to get below each company in which 'Selection' they are
Output:
MS 63 Swiss Re 63 Allianz 53 Liverpool 48 Manchester 15 Conoco 4 Zurich 3 HSBC 1 Goldman Sachs 1 Name: Company, dtype: int64
The expected result would be the following (for example):

Output:
MS 63 Topic 1 Topic 2 ... Swiss Re 63 Topic 23 Topic 55 ... Allianz 53 Liverpool 48 Manchester 15 Conoco 4 Zurich 3 HSBC 1 Goldman Sachs 1
Thank you in advance for your help!

Best,

T
Reply
#2
Nobody? :)
Reply
#3
One way to do what you want is to use multi-index, e.g.

import pandas as pd
df = pd.DataFrame({'Company': ['One', 'One', 'Two', 'Two', 'Two', 'Three'],
                   'value': [1, 3, 5, 7, 8, 0],
                   'tag': ['Table', 'Window', 'Plate', 'Board', 'Ship', 'Roof']})
df['counts'] = df.groupby('Company')['Company'].transform('count')
df.index = pd.MultiIndex.from_frame(df[['Company', 'tag']])
df.drop(['Company', 'value', 'tag'], axis=1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,336 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 53,446 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
Photo Creating column in python based on the other colums value count Bartek635 2 2,945 Apr-15-2021, 03:47 PM
Last Post: Bartek635
  How to scrape and count star rating using Selenium and Python? celinafregoso99 1 3,322 Feb-15-2021, 02:45 PM
Last Post: kashcode
  How to use the count function from an Excel file using Python? jpy 2 4,467 Dec-21-2020, 12:30 AM
Last Post: jpy
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 2,431 Oct-14-2019, 10:40 AM
Last Post: perfringo
  How to get a count of -Python- threads from the outside (or via code instrumentation) dstromberg 0 1,823 Jul-15-2019, 06:58 PM
Last Post: dstromberg
  To count a number of strings in a txt file with python jpeich 5 8,105 Apr-29-2017, 05:22 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020