I'm struglling concatenating some values contained in different columns of a Pandas.DataFrame
.
Given a DataFrame like this:
type | maker | detailsAsDict |
---|---|---|
car | jeep | {model: '1', color: 'red'} |
car | jeep | {model: '2', color: 'white'} |
truck | ford | None |
I want to change the value of maker
where the type
is car
, so that the DF would look like this:
type | maker | detailsAsDict |
---|---|---|
car | jeep, model: 1 | {model: '1', color: 'red'} |
car | jeep, model: 2 | {model: '2', color: 'white'} |
truck | ford | None |
To do this, I've tried using loc
:
cond = df['type'] == 'car'
df.loc[cond, 'maker'] = df['maker'] ', model:' df['detailsAsDict']['model']
However, this results in the error KeyError: model
.
So I tried the following:
def test_data(data):
print(data['model'])
return data['model']
df.loc[cond, 'maker'] = test_data(df['detailsAsDict'])
But then test_data
prints all None
values.
Is it possible to achieve what I'm trying to do? Am I missing something?
CodePudding user response:
You can use str
accessor to access values of dicts in a column:
cond = df['type'] == 'car'
df.loc[cond, 'maker'] = df['maker'] ', model:' df['detailsAsDict'].str['model']
Output:
type maker detailsAsDict
0 car jeep, model:1 {'model': '1', 'color': 'red'}
1 car jeep, model:2 {'model': '2', 'color': 'white'}
2 truck ford None
As for using test_data
to assign values, print
returns None, so you're really assigning None to df.loc[cond, 'maker']
.