-3
import pandas as pd 

stack = pd.DataFrame(['adam',25,28,'steve',25,28,'emily',18,21)
print(stack[0].to_list()[0::2]) 
print(stack[0].to_list()[1::2]) 
df = pd.DataFrame(
{'Name': stack[0].to_list()[0::3], 
'Age': stack[0].to_list()[1::3], 
'New Age': stack[0].to_list()[2::3] }
) 

print(df)

It how do i separate adam and steve into a different row? I want it to line up like the table below.

Table

  • 2
    on image you had different table – furas Mar 03 '20 at 03:56
  • Please post a good question as per [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). You must provide examples of what you are getting and what you want to be getting. – Ukrainian-serge Mar 03 '20 at 04:07
  • don't change original question. Now our answers doesn't fit to your question. If you have new problem then create new question on new page. – furas Mar 03 '20 at 04:57
  • you forgot `]` in lin `pd.DataFrame(['adam',25,28,'steve',25,28,'emily',18,21])` and it is all your problem. – furas Mar 03 '20 at 04:59

2 Answers2

0

You can get it as list and use slice [0::2] and [1::2]

import pandas as pd

data = pd.DataFrame(['adam',22,'steve',25,'emily',18])

print(data)
#print(data[0].to_list()[0::2])
#print(data[0].to_list()[1::2])

df = pd.DataFrame({
    'Name': data[0].to_list()[0::2],
    'Age': data[0].to_list()[1::2],
})

print(df)    

Before (like on original image which was removed from question)

       0
0   adam
1     22
2  steve
3     25
4  emily
5     18

After:

    Name  Age
0   adam   22
1  steve   25
2  emily   18

EDIT: image from original question

enter image description here


EDIT: BTW: the same with normal list

import pandas as pd

data = ['adam',22,'steve',25,'emily',18]

print(data)

df = pd.DataFrame({
    'Name': data[0::2],
    'Age': data[1::2],
})

print(df)    
furas
  • 134,197
  • 12
  • 106
  • 148
  • import pandas as pd stack = pd.DataFrame(['adam',25,28,'steve',25,28,'emily',18,21) print(stack[0].to_list()[0::2]) print(stack[0].to_list()[1::2]) df = pd.DataFrame({ 'Name': stack[0].to_list()[0::3], 'Age': stack[0].to_list()[1::3], 'New Age': stack[0].to_list()[2::3] }) print(df) i ran this but im confused as why it doesn't work – noobnoob123 Mar 03 '20 at 04:50
  • you forgot `]` in `pd.DataFrame([...])` – furas Mar 03 '20 at 04:53
0

These two lines should do it. However, without knowing what code you have, what you're trying to accomplish, or what else you intend to do with it, the following code is only valid in this situation.

d = {'Name': ['adam', 'steve', 'emily'], 'Age': [22, 25, 18]}
df = pd.DataFrame(d)
lincolnck
  • 302
  • 1
  • 12
  • The other answer is more robust, and will work in all cases where your original data (the left table) is able to be converted into a pandas dataframe. – lincolnck Mar 03 '20 at 03:56