Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to add data to the categorical index of dataframe as data arrives?
#1
Python 3.7.3, pandas 0.25.1

I need to collect statistics within the clients, so I created a df in which for each client I group the necessary data:

#create dataframe
dtype=np.dtype([('day_begin','u4'), ('day_end','u4'), ('price_begin','f4'), ('price_end','f4'), ('Client','U13')])      
auxiliary_array = np.empty(0, dtype=dtype)       
periods_clients = pd.DataFrame(auxiliary_array)        
periods_clients.set_index(['Client'], inplace=True)

#fill dataframe from file
with open(path_file) as csv_file:
        reader = csv.reader(csv_file, delimiter=';')
         
        fieldnames = ['Date', 'Client', 'Price']
        reader = csv.DictReader(csv_file, fieldnames=fieldnames, delimiter=';')
        for dict_str in reader:
            Client = dict_str['Client']
            
            if Client not in periods_clients.index:
                periods_clients.loc[Client] = [current_date, current_date, current_price, current_price]
            else:                  
                periods_clients.loc[Client].day_end = current_date
                periods_clients.loc[Client].price_end = current_price
The client is a string field, so the program runs for a very long time. My attempt to replace this field with a categorical variable failed, because I could not add values ​​while reading data from the file (and in advance I do not know all the clients).

How to be, boys?

if I create catregorical index, then I get error:

#create dataframe
dtype=np.dtype([('day_begin','u4'), ('day_end','u4'), ('price_begin','f4'), ('price_end','f4')])
auxiliary_array = np.empty(0, dtype=dtype)
periods_clients = pd.DataFrame(auxiliary_array)

periods_clients['Client'] = pd.Series('Client', dtype='category')
periods_clients.set_index(['Client'], inplace=True)
....
#fill dataframe from file
...
if Client not in periods_clients.index:
    periods_clients.index.add_categories(Client, inplace=True) #ERROR!!!
Error:
ValueError: cannot use inplace with CategoricalIn
Therefore, I cann't add values ​​to the categorical index as they become available.
Quote
#2
Maybe there is some way?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Getting Index Error - list index out of range RahulSingh 2 596 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  How to find index of a particular value in a dataframe ankitawadhwa 0 198 Jan-21-2020, 09:45 PM
Last Post: ankitawadhwa
  datetime intervals - dataframe selection (via plotted data) karlito 0 204 Nov-12-2019, 08:16 AM
Last Post: karlito
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 638 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  [pandas] Convert categorical data to numbers pradeep_as400 1 765 Jun-15-2019, 08:27 AM
Last Post: ThomasL
  Inserting data from python list into a pandas dataframe mahmoud899 0 616 Mar-02-2019, 04:07 AM
Last Post: mahmoud899
  Problem with saving data and loading data to mysql kirito85 4 898 Feb-08-2019, 10:53 AM
Last Post: kirito85
  How to get first and last row index of each unique names in pandas dataframe SriRajesh 1 1,916 Oct-13-2018, 07:04 AM
Last Post: perfringo
  Corrupt JPEG data: premature end of data segment MeenakshiChowdhary 0 2,766 Oct-12-2018, 11:55 AM
Last Post: MeenakshiChowdhary
  [SOLVED on SO] Downsizing non-representative data in DataFrame volcano63 1 680 Sep-28-2018, 12:56 PM
Last Post: volcano63

Forum Jump:


Users browsing this thread: 1 Guest(s)