I have a csv file that looks something like this
Time | OI | V |
---|---|---|
10:00:23 | 5.4 | 27 |
10:00:24 | -0.7 | 1 |
10:00:28 | -0.5 | 4 |
10:00:29 | 0.2 | 12 |
Can I somehow add new time values using Pandas while filling the columns with zeros or Nan? For the entire csv file.
What would have turned out something like that ?
Time | OI | V |
---|---|---|
10:00:23 | 5.4 | 27 |
10:00:24 | -0.7 | 1 |
10:00:25 | 0 | Nan |
10:00:26 | 0 | Nan |
10:00:27 | 0 | Nan |
10:00:28 | -0.5 | 4 |
10:00:29 | 0.2 | 12 |
CodePudding user response:
Convert column to datetimes, create DatetimeIndex
and add missing values by DataFrame.asfreq
, last replace NaN
s in OI
column:
df['Time'] = pd.to_datetime(df['Time'])
df = df.set_index('Time').asfreq('S').fillna({'OI':0})
df.index = df.index.time
print (df)
OI V
10:00:23 5.4 27.0
10:00:24 -0.7 1.0
10:00:25 0.0 NaN
10:00:26 0.0 NaN
10:00:27 0.0 NaN
10:00:28 -0.5 4.0
10:00:29 0.2 12.0
df['Time'] = pd.to_datetime(df['Time'])
df = df.set_index('Time').asfreq('S').fillna({'OI':0}).reset_index()
df['Time'] = df['Time'].dt.time
print (df)
Time OI V
0 10:00:23 5.4 27.0
1 10:00:24 -0.7 1.0
2 10:00:25 0.0 NaN
3 10:00:26 0.0 NaN
4 10:00:27 0.0 NaN
5 10:00:28 -0.5 4.0
6 10:00:29 0.2 12.0