How can I add values to the begining and end of rows in a Pandas Dataframe?

Question:

I have a .csv file that lists a couple thousand names. If I read the csv file into a pandas data frame is there an easy what to add quotes around the names with a ‘,’ at the end of each row?

Example below

This is what the output of the CSV file looks like now.
name1
name2
name3
name4

What I would like the output to look like with Pandas
‘name1’,
‘name2’,
‘name3’,
‘name4’,
etc

I would usually do this with Sublime or some other text editor however it has been freezing on me when I try to make a mass edit.

One thing I do want to point out is that the names in the csv file are all unique.
Also, I am only referencing pandas as it is all I am familiar with in regard to working with csv files in python.

The code I have so far looks like this and I am not sure how to proceed:

file = ('path/file.csv')
df = pd.read_csv(file)
print(df)
Asked By: dispatch

||

Answers:

  1. Assume that the column has a name such as df[‘name’]

  2. Use the following the add "’"
    df[‘newName’] = df[‘name’].apply(lambda x : "’" + x + "’")

  3. A new column ‘newName’ is created where the quote is added.

Answered By: Fong Sow Leong
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.