2
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.

2
  • 3
    Please don't name your lists as list Commented Oct 18, 2019 at 6:30
  • 2
    You can use itertools.cycle Commented Oct 18, 2019 at 6:31

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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)

2 Comments

It returns "can't multiply sequence by non-int of type 'float'", as the column N was inserted with value = np.nan, where np.nan is 'float' but not 'int'. In this case, I would like to ask how to change the values in column N to int? Thanks.
you have np.nan in the dataframe column, use df['Column N'].fillna(0, inplace=True) to replace nan value with 0
0
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.