1

in my data frame I want to iterrows() of two columns but want to save result in 1 column.for example df is

x       y      
5       10
30     445
70     32

expected output is

points      sequence
5             1
10            2 
30            1 
445           2
I know about iterrows() but it saved out put in two different columns.How can I get expected output and is there any way to generate sequence number according to condition? any help will be appreciated.
user19
  • 93
  • 9

2 Answers2

0

First never use iterrows, because really slow.

If want 1, 2 sequence by number of columns convert values to numy array by DataFrame.to_numpy and add numpy.ravel, then for sequence use numpy.tile:

df = pd.DataFrame({'points': df.to_numpy().ravel(),
                   'sequence': np.tile([1,2], len(df))})
print (df)
   points  sequence
0       5         1
1      10         2
2      30         1
3     445         2
4      70         1
5      32         2
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Do this way:

>>> pd.DataFrame([i[1] for i in df.iterrows()])
   points  sequence
0       5         1
1      10         2
2      30         1
3     445         2
Vicrobot
  • 3,795
  • 1
  • 17
  • 31