-1

I have one data frame and i am reading that with pandas python , in that one column contains dictonay values and i want to normalize that column i.e i want to split and i am using this:

if 'X' in df_cols:
    def only_dict(d):
    '''
    Convert json string representation of dictionary to a 
python dict
'''
return d

A = json_normalize(df['X'].apply(only_dict).tolist()).add_prefix('X.')
df = df.drop('X', axis=1).join(A)

but this column contains some null values too, so i want to apply only on column which have not null values using labmda is anyone have idea how to do it?

Dadep
  • 2,796
  • 5
  • 27
  • 40
  • 3
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jul 03 '18 at 06:14

1 Answers1

1

Just a code example. You can call to your own function using apply and lambda functions. You can access any column in your df using the parameter "axis=1". You con store the result of any transformation in a new column returning the desired value in the function and storing it in a new column.

def your_function(row): if row[your_column]: return row[your_column].split() else: return None

df['new_column'] = pandas.apply(lambda row: your_function(row),axis=1)

alvaro nortes
  • 570
  • 4
  • 10