How to write and loop a list on a column in python dataframe?

Viewed 346
list = [a, b, c, d, e, f, g, h]

How to write and loop the above list on a specific column in python dataframe, so that the outcome will be ...

Dataframe:

Column N (For example)

a   
b   
c   
d  
e  
f  
g  
h  
a  
b  
c  
d  
e  
f  
g  
h  
...  
(many times)   
...  
a  
b  
c  
d  
e  
f  
g  
h 

Or, is there any other way to perform the transformation above without the need of using a list? Thank you very much.

4 Answers

The times variable is how many times you want to repeat. Try the following:

import pandas as pd

letters = 'abcdefgh'
times = 4
df = pd.DataFrame({'Column Name': [x for x in letters]*times})
print(df)

or this:

import pandas as pd
from string import ascii_lowercase

times = 4
df = pd.DataFrame({'Column Name': [x for x in ascii_lowercase[:8]]*times})
print(df)

ascii_lowercase returns all the letters of the alphabet.

First dont use list for variable name, because builtin (python code word), and then for repeat use numpy.tile:

import numpy as np

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
N =  2
df = pd.DataFrame({'col': np.tile(L, N)})
print (df)
   col
0    a
1    b
2    c
3    d
4    e
5    f
6    g
7    h
8    a
9    b
10   c
11   d
12   e
13   f
14   g
15   h

You can repeat list with list * n where n is the number of times it repeats. Then assign it to 'Column N' of the dataframe, for example:

import pandas as pd
list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

df = pd.DataFrame()
df['Column N'] = list * 5
print(df)
import pandas as pd
import numpy as np
print(pd.DataFrame(columns=['column name'],data= ['1', np.nan, 'c', 'd', 'e', 'f', 'g', 'h']*5))
Related