i want to create a new dataframe using while loop.
The input is:
a=pd.DataFrame({'c':[1,3],'b':[10,20]})
I want to work on one row so i have selected that row:
s=a.loc[a['c']==3]
Now i want to create a new dataframe e using values in s with while loop
I want to apply condition that if s['c']<s['b'] add 2
And return s['b'] if the value >=s['b']
The required output should look like this:
e=pd.DataFrame([3,5,7,9,11,13,15,17,19,20])
#(i am not allowed to add picture thats why)
I have tried these codes:
e=pd.DataFrame(s['c'])
for i in e:
while e[i,0]<s['b']:
e[i,0]=e[i,0] 2
i=i 1
2nd try:
e=pd.DataFrame(s['c'])
for i in e:
while e[i,0]<s['b']:
e.iat[i,0]=e.iat[i,0] 2
i=i 1
CodePudding user response:
Pandas dataframes are normally used for manipulating existing data through unified operations on full columns/rows, not iterating over things, but you can accomplish the effect you're looking for by just generating the list normally and passing it into dataframe:
i = s['c'][1]
l = []
while True:
l.append(i)
i = 2
if i >= s['b'][1]:
l.append(s['b'][1])
break
e=pd.DataFrame(l)
