Reputation: 803
I am using pandas to read a CSV which contains a phone_number
field (string), however, I need to convert this field into the below JSON format
[{'phone_number':'+01 373643222'}]
and put it under a new column name called phone_numbers
, how can I do that?
Searched online but the examples I found are converting the all the columns into JSON by using to_json()
which is apparently cannot solve my case.
Below is an example
import pandas as pd
df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'],
'phone_number': ['+1 569-483-2388', '+1 555-555-1212', '+1 432-867-5309']})
Upvotes: 0
Views: 38
Reputation: 5451
use map function like this
df["phone_numbers"] = df["phone_number"].map(lambda x: [{"phone_number": x}] )
display(df)
Upvotes: 1