I needed to add single quotes around each item for 2 different columns in a pandas dataframe. One column is has integer values, the other has string values. Then I wanted to put the items with single quotes around them into a new column.
I tried multiple suggestions on stackoverflow using a for loop with numpy's savetxt method. (I don't need to use numpy) I tried Regex. Couldn't get it to exactly work.
import pandas as pd
import numpy as np
data = {"id": [101, 102, 103, 104, 105],
"person": ['Ty', 'Al', 'Lou', 'Tao', 'Mick']}
df = pd.DataFrame(data)
id_in_quotes=[] #Wanted to put the new items with single quotes into an empty list and put into a new column
person_in_quotes=[] #Wanted to put the new items with single quotes into an empty list and put into a new column
for x in df: #DOES NOT WORK
np.savetxt('text.txt',x, fmt='%r') #DOES NOT WORK
x.append(id_in_quotes)#DOES NOT WORK
In the end, wanted to see 4 columns: id, person, id_with_quotes, person_with_quotes. Columns id and person remain the same. Columns id_with_quotes, person_with_quotes are id and person with each item wrapped in single quotes.