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']})
use map function like this
df["phone_numbers"] = df["phone_number"].map(lambda x: [{"phone_number": x}] )
display(df)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.