【问题标题】:Dataframe and loops and lists数据框和循环和列表
【发布时间】:2021-01-28 23:01:43
【问题描述】:

我想知道是否有更简洁的方法来编写下面的代码。我基本上有一个包含三列的数据框:“日期”、“Ramp_1”、“Ramp_2”。我的目标是按月分隔数据(Ramp_1 和 Ramp_2)。日期包含以下格式的多年数据:%Y-%m-%d %H:%M。我创建了一个只有月份的新列,然后我使用 df.loc 创建了新的数据框:

mon=['jan','feb','mar','apr', 'may', 'jun','jul','aug','sep','oct','nov','dec']
for i in range(len(mon)):
    mon[i] = df.loc[df["month"]==(i+1), ["Ramp_1","Ramp_2"]]

我想创建 12 个新数据框,每个数据框以一个月命名。我最终没有这样做,而是创建了一个数据框列表。所以我手动写了这样的代码:

jan=mon[0]
feb=mon[1]
mar=mon[2]
apr=mon[3]
may=mon[4]
jun=mon[5]
jul=mon[6]
aug=mon[7]
sep=mon[8]
octo=mon[9]
nov=mon[10]
dec=mon[11]

我的问题是:有没有更简洁的写法?我知道有,但我一直无法弄清楚!

我也试过这样做:

mon=['jan','feb','mar','apr', 'may', 'jun','jul','aug','sep','oct','nov','dec']
for i in range(len(mon)):
    name = mon[i]
    name = df.loc[df["month"]==(i+1), ["Ramp_1","Ramp_2"]]

我的数据样本:

Dates  Ramp_1   Ramp_2  month
0     2016-01-01 02:00:00  -823.0  -566.47      1
1     2016-01-01 03:00:00  -899.0  -586.54      1
2     2016-01-01 04:00:00  -652.0  -473.33      1
3     2016-01-01 05:00:00  -304.0  -178.20      1
4     2016-01-01 06:00:00    99.0   273.08      1
...                   ...     ...      ...    ...
35045 2019-12-31 11:00:00  -613.0  -793.54     12
35046 2019-12-31 12:00:00  -311.0 -1159.81     12
35047 2019-12-31 13:00:00  -530.0  -964.18     12
35048 2019-12-31 14:00:00    79.0   538.85     12
35049 2019-12-31 15:00:00   181.0   574.21     12

【问题讨论】:

  • 您能否提供一个示例数据集作为逗号分隔的文本?
  • 是的!一分钟!
  • 日期 Ramp_1 Ramp_2 月 0 2016-01-01 02:00:00 -823.0 -566.47 1 1 2016-01-01 03:00:00 -899.0 -586.54 1 2 2016-01-01 04:00:00 -652.0 -473.33 1 3 2016-01-01 05:00:00 -304.0 -178.20 1 4 2016-01-01 06:00:00 99.0 273.08 1 ... ... ... . .. ... 35045 2019-12-31 11:00:00 -613.0 -793.54 12 35046 2019-12-31 12:00:00 -311.0 -1159.81 12 35047 2019-12-31 13:00:00 -530.0 -964.18 12 35048 2019-12-31 14:00:00 79.0 538.85 12 35049 2019-12-31 15:00:00
  • 请考虑发布您的数据,例如this
  • 这样更好吗?

标签: python pandas dataframe loops


【解决方案1】:
import pandas as pd

# Use your data instead of this sample df
df = pd.DataFrame({'Dates':['2016-01-01 02:00:00','2016-01-01 03:00:00','2016-01-01 04:00:00',
                            '2016-01-01 05:00:00','2016-01-01 06:00:00'],
                   'Ramp_1':[-823.0,-899.0,-652.0,-304.0,99.0],
                   'Ramp_2':[-566.47,-586.54,-473.33,-178.20,273.08]})

df['Dates'] = pd.to_datetime(df['Dates'], format="%Y-%m-%d %H:%M")

# This is a list with the 12 DataFrames that you want
dfs_by_month = [df[df['Dates'].apply(lambda x: x.month)==month] for month in range(1,13)]

【讨论】:

  • 嗨 PSK,感谢您的评论。这导致与我最初使用数据框列表编写的结果相似。我想知道是否有一种方法可以更简洁地将第二步写入代码,而不是输入 jan = dfs_by_month[0], feb= dfs_by_month[1],...
  • 您能解释一下为什么要在一个月后命名您的DataFrames 吗?字典可能是一个不错的选择。你可以创建一个类并使用setattr(),就像提到的here一样。您也可以使用exec(),但不建议使用here。真的取决于你为什么这样做。
【解决方案2】:

如果您想存储数据框以供以后调用,您会喜欢字典吗?

months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
          'jul', 'aug', 'sep', 'oct', 'nov', 'dec']

data = {m: df.loc[df['month'] == n, ['Ramp_1', 'Ramp_2']]
        for n, m in enumerate(months, 1)}

【讨论】:

  • 嗨蒂莫西,感谢您的回复。我还没有真正尝试过使用字典,所以这看起来很整洁。这实际上也导致了与我写的类似的结果。不知道有没有办法更简洁地将第二步写进代码中:jan = data["jan"], feb = data["feb"], ...
猜你喜欢
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
相关资源
最近更新 更多