-1

I have a variable whose name is Strike, in Strike variable values regularly change because it is under a loop.

This is my Dataframe enter image description here code example -

for i in range(len(df.strike)):
    Strike = df.strike.iloc[i]
    list1 = ['0-50', '50-100', '100-150'.......]
    list2 = [2000, 132.4, 1467.40, ..........]
    
    df = []  # Here i have to create dataframe

Strike contains values like - 33000, 33100, 33200, 33300....... so on it contains at least 145 values. which I want to make rows.

And I have two list which is also changing from time to time because it is also under a loop.

list1 = ['0-50', '50-100', '100-150'.......]

list1 I want to make columns.

and list2 contains numeric values -

list2 = [2000, 132.4, 1467.40, ..........]

enter image description here

I need dataframe in this format. List 1 should we column name, and list 2 should we values and strike variable should be rows. but I don't understand how can I create this data frame.

Krishna Gupta
  • 166
  • 1
  • 10
  • You example is not reproducible and unclear. Can you provide the full data that is in your picture? – mozway Mar 15 '22 at 08:50
  • @mozway in that picture it will be output dataframe. – Krishna Gupta Mar 15 '22 at 08:51
  • I understand, but the input is unclear/uncomplete – mozway Mar 15 '22 at 08:52
  • @mozway Well Strike is changing like 33000, 33100, 33200 ... like go on 145 values. and list 1 is a list which contains values at 6 or 7 strings. list 2 is a list too which contains numeric values which length is also similar to list 1. – Krishna Gupta Mar 15 '22 at 08:55
  • check [my answer](https://stackoverflow.com/a/71479375/16343464) and be more explicit if this is not what you want – mozway Mar 15 '22 at 08:56
  • In your example, you use `df.strike` , so I assume `strike` is a column of a dataframe. Is that correct? If so, please post the dataframe so we can play with it. Please check https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – alec_djinn Mar 15 '22 at 09:00

1 Answers1

0

IIUC you could use the DataFrame constructor directly with a reshaped numpy array as input:

# dummy example
list2 = list(range(4*7))

list1 = ['0-50', '50-100', '100-150', '150-200']

# replace by df.strike
strike = [33000, 33100, 33200, 33300, 33400, 33500, 33600]

df = pd.DataFrame(np.array(list2).reshape((-1, len(list1))),
                  index=strike, columns=list1)

output:

       0-50  50-100  100-150  150-200
33000     0       1        2        3
33100     4       5        6        7
33200     8       9       10       11
33300    12      13       14       15
33400    16      17       18       19
33500    20      21       22       23
33600    24      25       26       27
mozway
  • 194,879
  • 13
  • 39
  • 75
  • actually, I can't convert Strike into a list. And List 2 is changing again and again like list2 = [[2000, 132.4, 1467.40, 455.4], [222.15, 222.15, 218.4, 218.4 ], []] – Krishna Gupta Mar 15 '22 at 08:57
  • @Krishna thus the need for a reproducible example, provide a example `df`. Also, you can convert dataframe columns to list – mozway Mar 15 '22 at 08:59