【问题标题】:Fill other columns with missing dates Nan Pandas DataFrane用缺失的日期填充其他列 Nan Pandas DataFrame
【发布时间】:2021-05-29 15:32:26
【问题描述】:

我实际上是从几个监控我每日卡路里摄入量的 Excel 文件中提取数据。我设法使用列表理解来生成日期。我尝试使用合并或加入,但它不起作用。 ValueError: 您正在尝试合并 object 和 float64 列。

date_list = ['2021-05-22','2021-05-24','2021-05-26','2021-05-27']
idx = pd.date_range(date_list[0], date_list[-1]) # To find missing dates
df_dates = pd.DataFrame(idx) # To convert list to DataFrame
df1_dates = pd.DataFrame(np.repeat(df_dates.values,2,axis=0)) # However, there is no column title and it is default at 0.

我还有另一组关于我的卡路里摄入量和运动时间的数据。

# These are Lists.
Time = [Morning, Afternoon, Morning, Afternoon, Morning, Afternoon, Morning, Afternoon]
Calories = [420,380,390,400,350,280,300,430]
Duration = [50,40,45,50,45,50,44,58]

我面临的问题是我不知道如何在使用 np.repeat 后为 df1_dates 数据框创建列标题(“日期”)。我想用“NaN”填充与缺失日期相对应的其他列。

输出应该是这样的:

         Date       Time calories duration
0   22/5/2021    Morning      420       50
1   22/5/2021  Afternoon      380       40
2   23/5/2021    Morning      Nan      Nan
3   23/5/2021  Afternoon      Nan      Nan
4   24/5/2021    Morning      390       45
5   24/5/2021  Afternoon      400       50
6   25/5/2021    Morning      Nan      Nan
7   25/5/2021  Afternoon      Nan      Nan
8   26/5/2021    Morning      350       45
9   26/5/2021  Afternoon      280       50
10  27/5/2021    Morning      300       44
11  27/5/2021  Afternoon      430       58

【问题讨论】:

  • 您可以在构造函数中声明列名:pd.DataFrame(..., columns=[xxx])。另一种选择是像这样设置列名:df1_dates.columns = [xxx].

标签: python pandas dataframe


【解决方案1】:

使用现有数据构建您的数据框,并使用缺失的日期对其重新编制索引

# Input data
date_list = ['2021-05-22','2021-05-24','2021-05-26','2021-05-27']
calories = [420,380,390,400,350,280,300,430]
duration = [50,40,45,50,45,50,44,58]

# Dataframe with sparse index
idx = pd.MultiIndex.from_product([pd.to_datetime([d for d in date_list]),
                                  ["Morning", "Afternoon"]],
                                 names=["Date", "Time"])
df = pd.DataFrame({'calories': calories, 'duration': duration}, index=idx)

# Dataframe with full index
idx1 = pd.MultiIndex.from_product([pd.date_range(date_list[0], date_list[-1]),
                                   ["Morning", "Afternoon"]],
                                  names=["Date", "Time"])
df1 = df.reindex(idx1).reset_index()
>>> df1
         Date       Time  calories  duration
0  2021-05-22    Morning     420.0      50.0
1  2021-05-22  Afternoon     380.0      40.0
2  2021-05-23    Morning       NaN       NaN
3  2021-05-23  Afternoon       NaN       NaN
4  2021-05-24    Morning     390.0      45.0
5  2021-05-24  Afternoon     400.0      50.0
6  2021-05-25    Morning       NaN       NaN
7  2021-05-25  Afternoon       NaN       NaN
8  2021-05-26    Morning     350.0      45.0
9  2021-05-26  Afternoon     280.0      50.0
10 2021-05-27    Morning     300.0      44.0
11 2021-05-27  Afternoon     430.0      58.0

【讨论】:

  • 您好 Corralien,感谢您的回答!所以我不应该使用 np.repeat?
  • 你不需要np.repeatpd.MultiIndex.from_productDate['Morning', 'Afternoon'] 创建所有组合(np.repeat)。显示idxidx1查看MultiIndex的结构
  • 如果符合您的需要,请不要忘记接受回复。
  • 应该是df1 = df.reindex(idx).reset_index() 而不是idx1吧!
  • 很酷!感谢 Corralien 的大力帮助!!!谢谢阿努拉格!
猜你喜欢
  • 2022-08-15
  • 2021-09-03
  • 2020-11-14
  • 2021-08-11
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多