Skip to content Skip to sidebar Skip to footer

How To Replace Comma With Dash Using Python Pandas?

I have a file like this: name|count_dic name1 |{'x1':123,'x2,bv.':435,'x3':4} name2|{'x2,bv.':435,'x5':98} etc. I am trying to load the data into a dataframe and count the number

Solution 1:

You can use ast.literal_eval as a converter for loading the dataframe, as it appears you have data that's more Python dict-like... JSON uses double quotes - eg:

import pandas as pd
importastdf= pd.read_csv('file', delimiter='|', converters={'count_dic': ast.literal_eval})

Gives you a DF of:

    name                            count_dic
0  name1  {'x2,bv.': 435, 'x3': 4, 'x1': 123}
1  name2            {'x5': 98, 'x2,bv.': 435}

Since count_dic is actually a dict, then you can apply len to get the number of keys, eg:

df.count_dic.apply(len)

Results in:

0312Name:count_dic,dtype:int64

Solution 2:

Once df is defined as above:

# get a value to play around with
td = df.iloc[0].count_dic
td
# that looks like a dict definition... evaluate it?eval(td)
eval(td).keys() #yup!#apply to the whole df
df.count_dic = map(eval, df.count_dic)

#and a hint towards your key-countingmap(lambda i: i.keys(), df.count_dic)

Post a Comment for "How To Replace Comma With Dash Using Python Pandas?"