Python Forum
how to deal with varying value-counts().keys() in pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to deal with varying value-counts().keys() in pandas dataframe
#1
i ahve used following code to save value-counts in separate files.it work for two values.but my dataframe conatins maximum 4 keys ,it varies like(1978,1821), (1900,1978,1821,200)(1821,2000,1900). how can i modify the code to make it generic
start=0,end=100
 for i in range(start,len(df1),100):         
        df2=df1.iloc[start:end,]
        value=df2[2].value_counts().keys().tolist()
        counts=df2[2].value_counts().tolist()
        k=0
        for items in value :
             if items==1978 :
                 filewriterstock.writerow([start, counts[k]])
             if items==1821 :
                filewriterservive.writerow([start, counts[k]])
             k=k+1 

       start=end
       end=end+100
Reply
#2
That is not Python!
(May-16-2018, 06:19 AM)dilmailid Wrote:
        value=df2[2].value_counts().keys().tolist()
        counts=df2[2].value_counts().tolist()
        k=0
        for items in value :
             if items==1978 :
                 filewriterstock.writerow([start, counts[k]])
             if items==1821 :
                filewriterservive.writerow([start, counts[k]])
             k=k+1 

       start=end
       end=end+100

That is
for key, count in df2[2].value_counts().items():
    if value in {1872, 1978}:
        filewriterservive.writerow([start, count]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Thanks for that great piece of code. but it writes to the same file.. i need separate file and the i am not sure to use value in {1872, 1978} .values are not fixed
Reply
#4
(May-16-2018, 08:38 AM)dilmailid Wrote: Thanks for that great piece of code. but it writes to the same file.. i need separate file and the i am not sure to use value in {1872, 1978} .values are not fixed

It is not clear from your code:
  1. How do you select target values
  2. How do you select files
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
Each unique value in the dataframe is a target value. maximum is 4. for each value there is a file associated with it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 962 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,091 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,269 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,243 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,759 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,527 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,103 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,422 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,919 Jan-11-2021, 09:53 PM
Last Post: Azureaus

Forum Jump:

User Panel Messages

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