1

I have a list from which I would like to iterate over and create tuples in a pandas dataframe in order to imitate sliding window of size 4. What I am trying to do is:

tuples = pd.DataFrame()
for index, row in expertsDF.iterrows():
      newlst = row['name'] 
      counter = 0
      for x in newlst:
        if counter < len(newlst) - 3:
            tuples['A'] = x
            tuples['B'] =newlst(counter+1)
            tuples['C'] =newlst(counter+2)
            tuples['D'] =newlst(counter+3)
            counter = counter + 1

the newlst looks like this:

list (var1, var2, var3....)

and my DataFrame should be like this:

     A       B      C       D
1   var1   var2   var3    var4
2   var2   var3   var4    var5
3   var3   var4   var5    var6

Is there a way to do that in python?

5
  • could you post a sample of your output? Commented Oct 4, 2016 at 18:05
  • the code inside the if condition is not correct, therefore I have no output. I just tried to show what I am trying to do. Commented Oct 4, 2016 at 18:06
  • ok, @345243lkj done! Commented Oct 4, 2016 at 18:26
  • sorry, what is expertsDF.iterrows() ? Commented Oct 4, 2016 at 18:28
  • it is from library itertools. I used it to iterate through the initial dataframe where my values where stored. Commented Oct 4, 2016 at 18:30

2 Answers 2

1

Is this close?

import pandas as pd
tuples = pd.DataFrame(columns=['A', 'B', 'C', 'D'])
newlst = "abcdefg"
i = 0
for x in newlst:
    if i < len(newlst) - 3:
        t = pd.DataFrame([[x, newlst[i + 1], newlst[i + 2], newlst[i + 3]]],
                         columns=['A', 'B', 'C', 'D'])
    tuples = tuples.append(t, ignore_index=True)
    i += 1
print tuples

This prints:

   A  B  C  D
0  a  b  c  d
1  b  c  d  e
2  c  d  e  f
3  d  e  f  g
Sign up to request clarification or add additional context in comments.

Comments

1

How about creating the DataFrame from Series objects?

import pandas as pd

data_list = list(map(lambda x: 'var{}'.format(x),range(0,100)))
df = pd.Series(data_list[0:97]).to_frame(name='A')
df['B'] = pd.Series(data_list[1:98])
df['C'] = pd.Series(data_list[2:99])
df['D'] = pd.Series(data_list[3:100])

df.head()

# output
#       A     B     C     D
# 0  var0  var1  var2  var3
# 1  var1  var2  var3  var4
# 2  var2  var3  var4  var5
# 3  var3  var4  var5  var6
# 4  var4  var5  var6  var7

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.