6

I've a data frame genre_rail in which one column contains numpy.ndarray. The dataframe looks like as given belowdatframe

The array in it looks like this :

['SINGTEL_movie_22906' 'SINGTEL_movie_22943' 'SINGTEL_movie_24404'
 'SINGTEL_movie_22924' 'SINGTEL_movie_22937' 'SINGTEL_movie_22900'
 'SINGTEL_movie_24416' 'SINGTEL_movie_24422']

I tried with the following code

import json
json_content = json.dumps({'mydata': [genre_rail.iloc[i]['content_id'] for i in range(len(genre_rail))] })

But got an error

TypeError: array is not JSON serializable

I need output as

{"Rail2_contend_id":
["SINGTEL_movie_22894","SINGTEL_movie_22898",
"SINGTEL_movie_22896","SINGTEL_movie_24609","SINGTEL_movie_2455",
"SINGTEL_movie_24550","SINGTEL_movie_24548","SINGTEL_movie_24546"]}
2
  • Since you mention dataframe, there are also Pandas dataframe to json without index and Convert pandas dataframe to json format Commented Apr 11, 2017 at 12:54
  • The question is also outdated. By now pandas has df.to_json(PATH_HERE, orient='records'). Check out the function documentation regarding orient for various formats of the json structure. Commented Aug 1, 2018 at 14:22

2 Answers 2

21

How about you convert the array to json using the .tolist method. Then you can write it to json like :

np_array_to_list = np_array.tolist()
json_file = "file.json" 
json.dump(b, codecs.open(json_file, 'w', encoding='utf-8'), sort_keys=True, indent=4)
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your method, but it seems that the program stucked at tolist().
@yurenzhong tolist() has proceeded with very long computation time to me
8

Load all the data in dictionary, then dump it to json. Below code might help you

import json

#Data
d = ['SINGTEL_movie_22906', 'SINGTEL_movie_22943', 'SINGTEL_movie_24404'
 'SINGTEL_movie_22924', 'SINGTEL_movie_22937', 'SINGTEL_movie_22900'
 'SINGTEL_movie_24416', 'SINGTEL_movie_24422']

#Create dict
dic = {}
dic['Rail2_contend_id'] = d

print dic

#Dump data dict to jason
j = json.dumps(dic)

Output

{'Rail2_contend_id': ['SINGTEL_movie_22906', 'SINGTEL_movie_22943', 'SINGTEL_movie_24404SINGTEL_movie_22924', 'SINGTEL_movie_22937', 'SINGTEL_movie_22900SINGTEL_movie_24416', 'SINGTEL_movie_24422']}

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.