简体   繁体   English

附加到 Dataframe 然后按索引改组

[英]Appending to Dataframe then shuffling by index

I have an existing dataframe with some data.我有一个包含一些数据的现有数据框。 Something like就像是

Time, Price
15:00  100
15:01  125
15:03  121

where time is the index of the dataframe.其中时间是数据帧的索引。 However, because the dataframe is missing some pieces of data like the row at 15:02 in my example above, I wrote an algorithm that finds the price for these missing rows.但是,由于数据框缺少一些数据,例如上面示例中 15:02 的行,因此我编写了一个算法来查找这些缺失行的价格。 I now have a list of lists where each inner list is the time and price of missing data.我现在有一个列表列表,其中每个内部列表是丢失数据的时间和价格。 For example, it could be例如,它可能是

[[15:02,123]]

for this example.对于这个例子。 I plan to convert this list into a pandas dataframe, merge it back to the original dataframe and have it sorted by its index ( time ).我计划将此列表转换为 pandas 数据框,将其合并回原始数据框,并按其索引(时间)对其进行排序。 What is the best way to achieve this?实现这一目标的最佳方法是什么?

You should first create a dataframe out of your list with same columns.您应该首先从列表中创建一个具有相同列的数据框。 Then use the function:然后使用函数:

df = df.append(new_time_price_df,ignore_index=True)

And then use the method sort_values:然后使用方法sort_values:

df = df.sort_values(‘Time’)

Now df is sorted properly!现在 df 已正确排序!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM