jules325
jules325

Reputation: 179

Create a list of strings with consecutive numbers appended

I'm sure there must e an easy way to do this: I want to create a list which will eventually become column names for a DataFrame. There will be 13 columns, each representing a period in time, called "Period n", where n is the period number. I think there's probably a way to build this list via loop, but I will show what I tried to do:

col_no = list(range(1,14))
col_name = ['Period' for n in range (1,14)]
col_list = col_name + col_no

print(col_list)

['Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 'Period',
 1,
 2,
 3,
 4,
 5,
 6,
 7,
 8,
 9,
 10,
 11,
 12,
 13]

Then I tried:

col_list = list(zip(col_name + col_no))
print(col_list)

[('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 ('Period',),
 (1,),
 (2,),
 (3,),
 (4,),
 (5,),
 (6,),
 (7,),
 (8,),
 (9,),
 (10,),
 (11,),
 (12,),
 (13,)]

Basically, I just wanted an easily generated list that reads "Period 1", "Period 2", etc. I'm pretty new to Python, and pretty stumped.Thanks in advance

Upvotes: 7

Views: 11291

Answers (6)

Neil
Neil

Reputation: 14313

You can concatenate them (the number and the word Period) together while the loop iterates.

Python 3.6+

print([f'Period {i}' for i in range(1, 14)])

Python 2.7+

print(['Period {}'.format(i) for i in range(1, 14)])

Upvotes: 23

Yuca
Yuca

Reputation: 6091

List comprehension is the way to go:

solution = (['Period %i' %i for i in range(1, 14)])

Upvotes: 1

piRSquared
piRSquared

Reputation: 294238

Pandas has convenient methods pandas.DataFrame.add_prefix and pandas.DataFrame.add_suffix that do this for you.

import pandas as pd

df = pd.DataFrame(1, list('abc'), range(1, 5))
df

   1  2  3  4
a  1  1  1  1
b  1  1  1  1
c  1  1  1  1

df.add_prefix('Period_')

   Period_1  Period_2  Period_3  Period_4
a         1         1         1         1
b         1         1         1         1
c         1         1         1         1

Upvotes: 7

abarnert
abarnert

Reputation: 365697

You were on the right path in your last attempt:

zip(col_name + col_no)

The big problem is that + there. You concatenate the two lists together into one big list, and then try to zip that. What you wanted to do was zip the two lists:

zip(col_name, col_no)

… and then add the resulting pairs.

This means using a loop—whether a for statement, a list comprehension, the implicit loop inside map, ….

Also, you can’t just add a string and a number; you need to convert the number to a string first:

name + str(no)

… or use string formatting:

f"{name}{no}”

So, putting it all together:

[name + str(no) for name, no in zip(col_name, col_no)]

There are other ways to solve this (you don’t really need that list of a bunch of copies of the same name, for example), but this shows how to get there from where you were heading.

Upvotes: 5

iridescent
iridescent

Reputation: 383

You were appending a list of numbers to a list of Periods. To achieve what you want, you'd have needed to loop through the list of Periods and concatenate them with numbers 1 to 13. However a simpler solution exists so you can try this instead.

col_list = ['Period ' + str(n) for n in range(14)]
print col_list

Upvotes: 1

titiree
titiree

Reputation: 489

Try the following:

['Period ' + str(n) for n in range(14)]

Upvotes: 6

Related Questions