Variation of iterating through a single column
<p>I know there are a zillion ways to iterate through data in a data frame.
I am acquiring data from a detector, power, frequency, time. The time and power columns have values in every row. The frequency changes with time <strong>but</strong> for each frequency 'segment' the frequency and duty cycle are only listed in the column at the beginning of the segment. Below what it looks like.</p>

<pre class="lang-none prettyprint-override"><code>time power frequency duty cycle
1.4  1.2   500.0     45.0
2.1  49.9  NaN       NaN
3.4  245.0 NaN       NaN
4.5  323.0 NaN       NaN
5.6  320.0 NaN       NaN
6.6  309.0 1000      45
7.6  306.0 NaN       NaN
8.7  305.0 NaN       NaN
9.7  304.0 NaN       NaN
10.8 300.0 NaN       NaN
</code></pre>

<p>Using:</p>

<pre><code>InitFreqs = df['frequency']
InitDuty = df['dutycycle']
for i in np.arange(1, len(InitFreqs)):
    if np.isnan(InitFreqs[i]):
     InitFreqs[i] = InitFreqs[i - 1]
     InitDuty[i] = InitDuty[i - 1]
</code></pre>

<p>I get the result I want which looks like this:</p>

<pre class="lang-none prettyprint-override"><code>time power frequency duty cycle
1.4  1.2   500.0     45.0
2.1  49.9  500.0     45.0
3.4  245.0 500.0     45.0
4.5  323.0 500.0     45.0
5.6  320.0 500.0     45.0
6.6  309.0 1000      45.0
7.6  306.0 1000      45.0
8.7  305.0 1000      45.0
9.7  304.0 1000      45.0
10.8 300.0 1000      45.0
</code></pre>

<p>The 45 in this example might or might not change as well and subsequent values need to reflect this. While this does the job it is horribly slow and inefficient. I have found examples of how to replace <em>all</em> values in a column with something else or all NaN's but not exactly what I am looking for.
I should be able to perform this operation on a column as a whole vs the <code>for i</code> statement.</p>

<p>You want <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html" rel="nofollow"><code>fillna</code></a>:</p>

<pre><code>data = '''time power frequency duty_cycle
1.4  1.2   500.0     45.0 
2.1  49.9  NaN       NaN 
3.4  245.0 NaN       NaN 
4.5  323.0 NaN       NaN 
5.6  320.0 NaN       NaN 
6.6  309.0 1000      450 
7.6  306.0 NaN      NaN 
8.7  305.0 NaN      NaN 
9.7  304.0 NaN      NaN 
10.8 300.0 NaN      NaN'''

dd = [d.split() for d in data.split('\n')]
df = pd.DataFrame(dd[1:],columns=dd[0])
df.replace('NaN',np.nan,inplace=True)


df.fillna(method='ffill',axis=0)

time    power   frequency   duty_cycle
0   1.4     1.2     500.0   45.0
1   2.1     49.9    500.0   45.0
2   3.4     245.0   500.0   45.0
3   4.5     323.0   500.0   45.0
4   5.6     320.0   500.0   45.0
5   6.6     309.0   1000    450
6   7.6     306.0   1000    450
7   8.7     305.0   1000    450
8   9.7     304.0   1000    450
9   10.8    300.0   1000    450
</code></pre>

python python-3.x numpy dataframe