0

My current data frame looks like this:

      ID       Date            Data
00112   11-02-2014        {'address1': '161 Glensford Dr', 'address2': '', 'address3': '', 'city': 'Fayetteville', 'zip_code': '28314', 'country': 'US', 'state': 'NC', 'display_address': ['161 Glensford Dr', 'Fayetteville, NC 28314'], 'cross_streets': ''}
00112   11-02-2014       {...}
00112   30-07-2015       {...}
00112   30-07-2015       {...}

I would like to access to the column Data and get the value for all my rows of "address1" I have the same format for all my rows for this Data column:

{'address1': '1909 Skibo Rd', 'address2': '', 'address3': '', 'city': 'Fayetteville', 'zip_code': '28314', 'country': 'US', 'state': 'NC', 'display_address': ['1909 Skibo Rd', 'Fayetteville, NC 28314'], 'cross_streets': ''}

I do not how to get the value of address1 for each row.

coding2
  • 47
  • 6
  • https://stackoverflow.com/questions/35711059/extract-dictionary-value-from-column-in-data-frame Look at it. This should help. – Aniket Kumar Jul 07 '22 at 03:49

1 Answers1

0

Start by looping through the rows of the Data column:

for idx, row in df.iterrows():

Then get the data from dictionary using the standard dictionary get function:

print(idx, row['Data'].get("address1"))

So it's just 2 lines:

for idx, row in df.iterrows():
    print(idx, row['Data'].get("address1"))

Edit: if you already have a series assigned to the correct column:

series = df["Data"]
for idx, each in series.iteritems():
    print(idx, each.get("address1"))
M B
  • 2,700
  • 2
  • 15
  • 20
  • I got this AttributeError: 'Series' object has no attribute 'iterrows' – coding2 Jul 07 '22 at 03:56
  • I used `df` to refer to your entire dataframe, not a series within the dataframe. If you already have a series variable then you should use `iteritems` like this: `for idx, each in .iteritems():`. Also replace `row['Data']` with `each` – M B Jul 07 '22 at 04:03
  • I have edited the answer to include this – M B Jul 07 '22 at 04:12
  • Thank you, I am still having this AttributeError: 'str' object has no attribute 'get' – coding2 Jul 07 '22 at 04:18
  • I change my column to string df['Data'] = df['Data'].astype(str) – coding2 Jul 07 '22 at 04:18
  • Strings don't have a `get` function, only dictionaries do. So there is no reason for you to use `.astype(str)`. Remove that and it should work. – M B Jul 07 '22 at 04:55
  • Yes, but is not working. I am only importing my csv. My column data is type 'Object' this is an example for one row `{'address1': '161 Glensford Dr', 'address2': '', 'address3': '', 'city': 'Fayetteville', 'zip_code': '28314', 'country': 'US', 'state': 'NC', 'display_address': ['161 Glensford Dr', 'Fayetteville, NC 28314'], 'cross_streets': ''}`I do not why this is not working :( – coding2 Jul 07 '22 at 05:08
  • Since you haven't provided code I can't tell. Take a look at the example I reproduced here: online-python.com/OaS9HfPbqG and try to match your code to this. – M B Jul 07 '22 at 05:17