'Iterate through subsets of a dataframe
This is my first time posting on here. I have the code below that should loop through the energies in this dataframe and output the associated cross sections from the energy that falls within that range. However when I print out the cross-sections within the if statement, it prints out all the values in the energy dataframe, rather than a small subset. It should return only 120 cross section values in the end, not just one, as it does now. Does anyone know how I can fix this?
energy = df.x #pandas dataframe
def crosssection(energy):
for i in energy:
if [(i >= .284) & (i <.400)]:
c0, c1, c2 = 78.1, 18.8, 4.3
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
print(i)
print(cross_section)
elif [(i >= .400) & (i <.532)]:
c0, c1, c2 = 71.4, 66.8, -51.4
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= .532) & (i <.707)]:
c0, c1, c2 = 95.5, 145.8, -61.1
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= .707) & (i <.867)]:
c0, c1, c2 = 308.9, -380.6, 294.0
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= .867) & (i <1.303)]:
c0, c1, c3 = 120.6, 169.3, -47.7
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 1.303) & (i <1.840)]:
c0, c1, c3 = 141.3, 146.8, -31.5
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 1.840) & (i <2.471)]:
c0, c1, c3 = 202.7, 104.7, -17.0
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 2.471) & (i <3.210)]:
c0, c1, c3 = 342.7, 18.7, 0
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 3.210) & (i <4.038)]:
c0, c1, c3 = 352.2, 18.7, 0
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 4.038) & (i <7.111)]:
c0, c1, c3 = 433.9, -2.4, 0.75
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 7.111) & (i <8.331)]:
c0, c1, c3 = 629.0, 30.9, 0.0
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 8.331) & (i <10.0)]:
c0, c1, c3 = 701.2, 25.2, 0.0
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
elif [(i >= 10.0)]:
c0, c1, c3 = 701.2, 25.2, 0.0
cross_section = (c0 + c1*i+ c2*i**2)*(i**-3)*10**-24
return cross_section
Solution 1:[1]
Your return statement is in the for loop, so after the first energy, it will just return. What you probably want is to aggregate the individual cross_section values per energy and return a list or some collection of them. My example below is shortened but should give you the gist of it
EDIT: comments for clarity
Try:
def cross_section(energy):
cross_sections = []
for i in energy:
if [(i >= .284) & (i <.400)]:
c0, c1, c2 = 78.1, 18.8, 4.3
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
print(i)
print(cross_section)
elif [(i >= .400) & (i <.532)]:
c0, c1, c2 = 71.4, 66.8, -51.4
cross_section = (c0 + c1*i + c2*i**2)*(i**-3)*10**-24
# aggregate the individual cross_sections into a list
cross_sections.append(cross_section)
# return the list after looping
return cross_sections
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | myz540 |
