0

I have following dataframe :

state city alt_city FL FT. PIERCE FORT PIERCE FL FT. PIERCE FORTPIERCE FL FT. PIERCE FT.PIERCE FL FORT PIERCE FORTPIERCE FL FORT PIERCE FT PIERCE FL FORT PIERCE FT. PIERCE FL FORT PIERCE FT.PIERCE FL FORT PIERCE FTPIERCE AK ANCHORAGE ANCH AK ANCHORAGE ANCHORAGE 

and I want to genearte a dictionary from it based on certain conditions:

Following is a pseudo code :

def map_df_to_dict(d,state,city,alt_city): key1 = (state,city) val1 = alt_city key2 = (state,alt_city) val2 = city if key1 in d: d[key1].append(val1) else: d[key1] = [] d[key1].append(val1) if key2 in d: d[key2].append(val2) else: d[key2] = [] d[key2].append(val2) return d 

If I apply it on dataframe using following code :

cs_d = {} cs_dict = df.apply(lambda x: map_df_to_dict(cs_d,x['state'],x['city'],x['alt_city']), axis=1) 

then it will return dictionary for every row in the dataframe.

But how can I apply this on a dataframe so that it will not return weird results.

2
  • What is expected output? Commented Jul 17, 2018 at 6:44
  • I've added expected output Commented Jul 17, 2018 at 8:04

2 Answers 2

1

Dataframe.apply runs the function for each row (or column). That's why you get multiple dicts.

https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.DataFrame.apply.html

DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)

Applies function along input axis of DataFrame.

Objects passed to functions are Series objects having index either the DataFrame’s index (axis=0) or the columns (axis=1).

Parameters: func : Function to apply to each column/row

Sign up to request clarification or add additional context in comments.

Comments

0

You can get a single dictionary by declaring the dictionary "globally" outside the scope of your function. Consider the following pseudo code to add 2 to each number in a pandas Series.

test_a = pd.Series([1,2,3]) global_dict = {} def add_2(x): global_dict[x] = (x+2) 

When I apply this function on test_a, I get the below result:

test_a.apply(add_2) print(global_dict) 

global_dict = {1: 3, 2: 4, 3: 5}

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.