1

I have a quick question regarding data frames. Say I have a dataframe that outputs:

      City   Color State
0   Dallas     Red    TX
1  Seattle    Blue    WA
2   Boston  Purple    MA
3  Concord  Yellow    NH

How do I loop through the rows of the dataframe so that I can get this output?

'Dallas', 'Red', 'TX'
'Seattle', 'Blue', 'WA'
'Boston', 'Purple', 'MA'
'Concord', 'Yellow', 'NH'

Similarly, if I have a dataframe that just contains one column such as:

      City  
0   Dallas     
1  Seattle  
2   Boston  
3  Concord 

Is there a way to just output this?

'Dallas'
'Seattle'
'Boston'
'Concord'

My original code was looping through the dataframe using df.values, and then adding the "'" character at the beginning and end while stripping the bracket character. However, this obviously only works for just one column, and I would rather the code work if I continued to have N amount of columns.

Any help is greatly appreciated!

1
  • From the duplicate for index, row in df.iterrows(): print("'{}', '{}', '{}'".format(row.City, row.Color, row.State)) or for index, row in df.iterrows(): print(f"'{row.City}', '{row.Color}', '{row.State}'") Commented Feb 22, 2021 at 8:22

3 Answers 3

1

Use pandas.DataFrame.to_csv with quoting and quotechar (with header and index):

print(df.to_csv(header=False, index=False, quoting=csv.QUOTE_ALL, quotechar="'"))

Output:

'Dallas','Red','TX'
'Seattle','Blue','WA'
'Boston','Purple','MA'
'Concord','Yellow','NH'

Note that quoting is controlled using csv module:

csv.QUOTE_MINIMAL == 0
csv.QUOTE_ALL == 1
csv.QUOTE_NONNUMERIC == 2
csv.QUOTE_NONE == 3
Sign up to request clarification or add additional context in comments.

5 Comments

Ah this is perfect! I didn't know the .to_csv could be used in this way. Is there a way to not print out the first line/columns though?
@EyeOfTheOwl I've updated my post. Please give it a try ;)
Awesome, works great! Just one more question if you don't mind. Is there a way to output specific lines, or output each line one by one? I notice if you assign the df.to_csv to a variable, it will recognize the entire output as one string, but is there a way to make it 4 individual strings?
Assign it to some variable and then do some_variable.splitlines() :)
You're amazing- thanks so much! :-)
1

You can do what Chris already shared or you can also do it this way.

custom_format = {'City':'\'{}\','.format,'Color':'\'{}\','.format,'State':'\'{}\''.format}
print (df.to_string(formatters=custom_format,justify='left',header=False,index=False))

Here I am setting the format I want the output to be printed. Since you want a single quote and a comma, I am adding that to the format.

This will print the dataframe as follows:

  'Dallas',     'Red',  'TX'
 'Seattle',    'Blue',  'WA'
  'Boston',  'Purple',  'MA'
 'Concord',  'Yellow',  'NH'

You can decide to assign this to a variable.

It will be stored as follows:

"  'Dallas',     'Red',  'TX'\n 'Seattle',    'Blue',  'WA'\n  'Boston',  'Purple',  'MA'\n 'Concord',  'Yellow',  'NH'"

Or as Chris already shared, you can use .to_csv as follows:

import csv
print (df.to_csv(header=None, index=False,sep=",",quotechar="'",quoting=csv.QUOTE_ALL))

This will output as:

'Dallas','Red','TX'
'Seattle','Blue','WA'
'Boston','Purple','MA'
'Concord','Yellow','NH'

Assigned to a variable, it will give you the value as:

"'Dallas','Red','TX'\n'Seattle','Blue','WA'\n'Boston','Purple','MA'\n'Concord','Yellow','NH'\n"

You can also use df.values.tolist() but it does not add a ' and comma separate each value. You need to do a bit more processing.

Here's the way to address it:

z = ["'" + "','".join(x) + "'" for x in df.values.tolist()]
print (z)

The output of this will be:

["'Dallas','Red','TX'", "'Seattle','Blue','WA'", "'Boston','Purple','MA'", "'Concord','Yellow','NH'"]

This option will allow you to extract the data by index value. So if you want the first one, you can just give z[0]

Comments

1

Dataframe.iterrows() should do the trick for you!

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html

For individual Series you can do:

Dataframe[‘SeriesName’].iterrows()

I think you can also do list(Dataframe[‘SeriesName’]) as well but I’m on my phone and can’t test it.

#################

[Edit] It was suggested I add some examples. Now that I'm on a computer I've added a few below.

Firstly, in defense of using iterrows. The OP stated that he wanted to "loop" through the dataframe. Though there are better ways to accomplish this the iterrows method is one of the simpler ways to iterate through a dataframe piece by piece.

df = pd.DataFrame([
    ['Dallas','Red','TX'],
    ['Seattle', 'Blue', 'WA'],
    ['Boston', 'Purple', 'MA'],
    ['Concord', 'Yellow', 'NH']
], columns=['City', 'Color','State'])

for _, row in df.iterrows():
    city = row[0]
    color = row[1]
    state = row[2]
    print(f"{city}, {color}, {state}")

Which has an output of:

Dallas, Red, TX
Seattle, Blue, WA
Boston, Purple, MA
Concord, Yellow, NH

Also, if you have a single Series and you want to output the data you can use this:

for city in df['City']:
    print(f"'{city}'")

Which outputs:

'Dallas'
'Seattle'
'Boston'
'Concord'

There does appear to be some resistance against using iterrows among the comments and to_csv seems like a more elegant way to output it since you do not need to worry about the indexing and all that. But this may help someone out there.

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.