Reputation: 137
My initial dataframe df
:
discharge1 discharge2
datetime
2018-04-25 18:37:00 5862 4427
2018-04-25 21:36:30 6421 4581
2018-04-25 22:13:00 5948 4779
2018-04-26 00:11:30 5703 4314
2018-04-26 02:27:00 4988 3868
2018-04-26 04:28:30 4812 3823
2018-04-26 06:22:30 4347 3672
2018-04-26 10:50:30 3896 3546
2018-04-26 12:04:30 3478 3557
2018-04-26 14:02:30 3625 3598
2018-04-26 15:31:30 3751 3606
What I want to do is to get my dates a list, array, or series where I can iterate over all elements in my list. So that I can use those dates to access rows in another dataframe df_other
, and in the end append them to a new dataframe df_new
:
for date in date_list():
df_new = df_new.append(df_other.iloc[df_other.index.get_loc(date)])
which for a date on my list should be run as:
df_new.append(df_other.iloc[df_other.index.get_loc('2018-04-25 18:37:00')])
I tried making a list using df.index
but that returns a Datetimeindex
where I can only access each date as:
display(df.index[0])
Timestamp('2018-04-25 18:37:00')
where the Timestamp part ruins my .append
call.
Also tried df.index.tolist()
but that returns a list of: [Timestamp('2018-04-25 18:37:00'), ...]
Upvotes: 7
Views: 15311
Reputation: 2245
Why don't you just iterate over the rows of the dataframe and just use the index values?
create dataframe:
data = [
['2018-04-25 18:37:00', 5862, 4427],
['2018-04-25 21:36:30', 6421, 4581],
['2018-04-25 22:13:00', 5948, 4779],
['2018-04-26 00:11:30', 5703, 4314],
['2018-04-26 02:27:00', 4988, 3868],
['2018-04-26 04:28:30', 4812, 3823],
['2018-04-26 06:22:30', 4347, 3672],
['2018-04-26 10:50:30', 3896, 3546],
['2018-04-26 12:04:30', 3478, 3557],
['2018-04-26 14:02:30', 3625, 3598],
['2018-04-26 15:31:30', 3751, 3606]
]
data = pd.DataFrame(data, columns=['datetime', 'discharge1', 'discharge2'])
data['datetime'] = data['datetime'].apply(pd.to_datetime)
data = data.set_index('datetime')
then iterate over index, and values:
for index, values in data.iterrows():
print(index)
output:
2018-04-25 18:37:00
2018-04-25 21:36:30
2018-04-25 22:13:00
2018-04-26 00:11:30
...
Upvotes: 7