I'm crossing the information into two tables. I want to get the information from the column Register Number
in the table profile
and put it into the column Delivers
, which I just created, into the table consumption
, by using the information into the columns Cod. Deliver profile
and Cod. agent profile
- which is the same for the same delivers. I do it by using the following code:
consumption['Delivers'] = ''
for idx, x in consumption['Cod. Deliver profile'].iteritems():
consumption['Delivers'].iloc[idx] = profile.loc[profile['Cod. agent profile'] == x, 'Register Number'].values
The problem is that the method .values
returns a Numpy array
, which is not what I want - as below. It should have just one value of type string per cell and when there is no value to be set, empty or something else - but no brackets!
29 []
30 []
31 [6981180000116.0]
42 [357038000116.0]
43 []
44 [28152650000171.0]
If I use .item()
I get the following error:
ValueError: can only convert an array of size 1 to a Python scalar
If I use .iloc[0]
I get the following:
IndexError: single positional indexer is out-of-bounds
How to workaround this?
As pointed out by Rutger in comments, after you create your consumption['Delivers']
column which contains arrays as values, you just need to use apply
method to extract the values and convert them to string type, and '0'
in case of an empty array:
consumption['Delivers'] = ''
for idx, x in consumption['Cod. Deliver profile'].iteritems():
consumption['Delivers'].iloc[idx] = profile.loc[profile['Cod. agent profile'] == x, 'Register Number'].values
consumption['Delivers'] = consumption['Delivers'].apply(lambda x: str(x[0]) if len(x) > 0 else '0')