【问题标题】:Python Defined Functions with For Loops [duplicate]带有 For 循环的 Python 定义函数 [重复]
【发布时间】:2018-02-22 02:19:21
【问题描述】:

我有以下 DataFrame(这是一个精简版 - 它可以追溯到很长时间)

Week Commencing     A1    A2    A3      A4
2016-01-03          28    1375  1999    1345
2016-01-10          48    1552  2428    1337
2016-01-17          43    1895  2615    1420
2016-01-24          29    1950  2568    1385
2016-01-31          41    1912  2577    1277
2016-02-07          29    2176  2771    1403
2016-02-14          50    2229  3013    1450
2016-02-21          60    2271  3029    1489
2016-02-28          43    2140  3133    1594
2016-03-06          51    2080  3140    1498

我想创建一个新列,用于指定基于特定时间段的标签。 IE:如果行在某个日期之前,则返回一个单词。

我尝试了以下方法:

def action(x):
    if x == "True":
        return "Before Migration"
    if x == "False":
        return "After Migration"

df.index.apply(action, axis=1)

我收到以下错误:“AttributeError: 'DatetimeIndex' 对象没有属性 'apply'”。

我已将其更改为字符串,尝试重置索引以便我可以应用于列而不是索引,但它不起作用。

我也试过这个:

if df.index < '2016-02-14':
    df["Migration_Type"] = "Before Migration"
else:
    df["Migration_Type"] = "After Migration"

错误:ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()

任何关于更好方法的建议也很感激。

【问题讨论】:

  • 我使用了这个并且它有效 - 谢谢你。有时很难知道要搜索什么。如果需要,很高兴您删除。对此表示歉意

标签: python function pandas apply


【解决方案1】:

试试这样的:

# First, initialize a new column, set it to before migration by default
df = df.assign(Migration_Type = "Before Migration")
# Then, assign "after migration" to all rows after your chosen date
df.loc[df.index >= '2016-02-14', 'Migration_Type'] = "After Migration"

【讨论】:

  • 这非常有效。很好也很简单。更多是为了我自己的学习,但我的方法有什么问题吗?你能得到类似的结果吗?
  • 它实际上与您在第二次尝试(循环)时尝试做的非常相似,但以更像熊猫(矢量化)的方式。就您的 apply 方法而言,您调用 .apply 作为数据框索引上的方法,它没有这样的方法。 .apply 适用于将简单函数应用于数据框或系列,其中可以轻松隐含参数。如果 x 为 True,您的函数只是返回“迁移前”,但甚至没有任何迹象表明 x 是什么,也没有任何迹象表明“迁移前”应该返回到哪里。
  • 非常感谢您花时间帮助我。如果不是太麻烦,你能告诉我你将如何使用 for 循环(但正确的方法)来做到这一点 - 你已经帮助了我足够多,所以不要觉得有义务。
  • 这会起作用(如果你可以暗示缩进),但不推荐......会很慢而且很笨重。 df['Migration_Type'] = ""for i, row in df.iterrows(): if i &gt;= '2016-02-14': row['Migration_Type'] = "After Migration" else: row['Migration_Type'] = "Before Migration"
【解决方案2】:
df.assign(
    Migration_Type=np.where(
        df.index < '2016-02-14',
       'Before Migration',
       'After Migration'
    )
)


                 A1    A2    A3    A4    Migration_Type
Week Commencing                                        
2016-01-03       28  1375  1999  1345  Before Migration
2016-01-10       48  1552  2428  1337  Before Migration
2016-01-17       43  1895  2615  1420  Before Migration
2016-01-24       29  1950  2568  1385  Before Migration
2016-01-31       41  1912  2577  1277  Before Migration
2016-02-07       29  2176  2771  1403  Before Migration
2016-02-14       50  2229  3013  1450   After Migration
2016-02-21       60  2271  3029  1489   After Migration
2016-02-28       43  2140  3133  1594   After Migration
2016-03-06       51  2080  3140  1498   After Migration

【讨论】:

  • 这也是一种很好的方法——它也很有效。谢谢
猜你喜欢
  • 1970-01-01
  • 2013-01-25
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 2018-03-04
相关资源
最近更新 更多