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
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 283 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  [pandas] Convert categorical data to numbers pradeep_as400 1 368 Jun-15-2019, 08:27 AM
Last Post: ThomasL
  Inserting data from python list into a pandas dataframe mahmoud899 0 417 Mar-02-2019, 04:07 AM
Last Post: mahmoud899
  Problem with saving data and loading data to mysql kirito85 4 516 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,001 Oct-13-2018, 07:04 AM
Last Post: perfringo
  Corrupt JPEG data: premature end of data segment MeenakshiChowdhary 0 1,640 Oct-12-2018, 11:55 AM
Last Post: MeenakshiChowdhary
  [SOLVED on SO] Downsizing non-representative data in DataFrame volcano63 1 505 Sep-28-2018, 12:56 PM
Last Post: volcano63
  DataFrame index issue Astrikor 2 759 Aug-25-2018, 04:25 PM
Last Post: Astrikor
  Pandas nested json data to dataframe FrankC 1 4,989 Aug-14-2018, 01:37 AM
Last Post: scidam
  Trying to import JSON data into Python/Pandas DataFrame then edit then write CSV Rhubear 0 1,093 Jul-23-2018, 09:50 PM
Last Post: Rhubear

Forum Jump:


Users browsing this thread: 1 Guest(s)