Python Forum
Pandas Import CSV count between numerical values within 1 Column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas Import CSV count between numerical values within 1 Column
#2
I managed to get a workaround but I don't think this this is the best practice:

import pandas as pd
tsjune = pd.read_csv("/Users/phil/Documents/datacsv/tsj.csv", delimiter = ',')

one = tsjune[(tsjune.Difference >= 0) & (tsjune.Difference <=1)]
two = tsjune[(tsjune.Difference > 1) & (tsjune.Difference <=3)]
three = tsjune[(tsjune.Difference > 3) & (tsjune.Difference <30)]
four = tsjune[tsjune.Difference >= 30]


onecount = one.count()
twocount = two.count()
threecount = three.count()
fourcount = four.count()

total = onecount + twocount + threecount + fourcount

onepercentage = round(onecount/total*100, 2)
twopercentage = round(twocount/total*100, 2)
threepercentage = round(threecount/total*100, 2)
fourpercentage = round(fourcount/total*100, 2)

print(onepercentage['Difference'])
print(twopercentage['Difference'])
print(threepercentage['Difference'])
print(fourpercentage['Difference'])
Which gives me a percentage output of:

26.89
28.29
32.8
12.01

Any better ways of writing this query?
Reply


Messages In This Thread
RE: Pandas Import CSV count between numerical values within 1 Column - by ptaylor520 - Jul-15-2019, 04:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning conditional values in Pandas Scott 3 828 Dec-19-2023, 03:10 AM
Last Post: Larz60+
  HTML Decoder pandas dataframe column mbrown009 3 1,061 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  attempt to split values from within a dataframe column mbrown009 8 2,383 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  Increase df column values decimals SriRajesh 2 1,119 Nov-14-2022, 05:20 PM
Last Post: deanhystad
  pandas column percentile nuncio 7 2,476 Aug-10-2022, 04:41 AM
Last Post: nuncio
  import csv adding a header with pandas Soares 0 1,568 Dec-16-2021, 12:16 PM
Last Post: Soares
  pandas: Compute the % of the unique values in a column JaneTan 1 1,791 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,327 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 2,707 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  Help with Scipy optimize for numerical problem Jbjbjb1 0 1,566 Jun-22-2021, 05:03 AM
Last Post: Jbjbjb1

Forum Jump:

User Panel Messages

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