【问题标题】:Why am I Gettting all NAN while Appending a new Field to a Data Frame?为什么我在将新字段附加到数据框时得到所有 NAN?
【发布时间】:2021-01-10 13:59:59
【问题描述】:

这对我来说很奇怪。我有一个数据框,其中的字段具有文件名,如下所示。

df['file']

文件名如下所示。

0     FFIEC CDR Call Schedule RCB02 03312011.txt
1     FFIEC CDR Call Schedule RCB02 03312011.txt
2     FFIEC CDR Call Schedule RCB02 03312011.txt
3     FFIEC CDR Call Schedule RCB02 03312011.txt
4     FFIEC CDR Call Schedule RCB02 03312011.txt
5     FFIEC CDR Call Schedule RCB02 03312011.txt

我怎样才能解析出最后两个空格之间的最后一个字符串,所以看起来像这样。

RCB02

我正在尝试将其附加到数据框中,就像这样。

grouped_and_summed['schedule_code'] = df['file'].str[24:27]

当我查看数据框时,我有所有的 NAN。

我想要的是让grouped_and_summed 数据框有RCB02,正确显示,并显示为数据框中的第一列。我该怎么做?

【问题讨论】:

    标签: python python-3.x dataframe group-by


    【解决方案1】:

    您可以使用Series.str.extract方法和一些简单的正则表达式,然后将新列设置为结果集的.values

    df.loc[:, "schedule_code"] = df["file"].str.extract(r"FFIEC CDR Call Schedule (\w+) \d+\.txt").values
    

    输出 -

                                             file schedule_code
    0  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    1  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    2  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    3  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    4  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    5  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    

    验证

    它确实使事情保持一致。这是最后一个 RCB 值不同的另一个数据框:

                                             file
    0  FFIEC CDR Call Schedule RCB02 03312011.txt
    1  FFIEC CDR Call Schedule RCB02 03312011.txt
    2  FFIEC CDR Call Schedule RCB02 03312011.txt
    3  FFIEC CDR Call Schedule RCB02 03312011.txt
    4  FFIEC CDR Call Schedule RCB02 03312011.txt
    5  FFIEC CDR Call Schedule RCB03 03312011.txt # I'm different!
    

    输出是:

                                             file schedule_code
    0  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    1  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    2  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    3  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    4  FFIEC CDR Call Schedule RCB02 03312011.txt         RCB02
    5  FFIEC CDR Call Schedule RCB03 03312011.txt         RCB03 # Still here!
    

    【讨论】:

    • 这看起来像要走的路!!但是,当我尝试将其附加到我的数据框时,如下所示: grouped_and_summed.loc[:, "schedule_code"] = df["file"].str.extract(r"FFIEC CDR Call Schedule (\w+) \d+ \.txt").values 我收到此错误消息: ValueError: cannot set using a multi-index selection indexer with a different length than the value 另外,这个:grouped_and_summed["schedule_code"] = df["file"].str .extract(r"FFIEC CDR Call Schedule (\w+) \d+\.txt").values 给我这个错误:ValueError:值的长度(24)与索引的长度(96)不匹配
    • 嗯...这些错误是有道理的。看起来您的 grouped_and_summed 有一个像 "RCONG306", "RCONG307", ... 这样的索引,所以索引值不会匹配。索引RCONG306 的示例"schedule_code" 值是什么? df中是否还有其他匹配的字段(列)?
    • 如果我回到原始 df,在名为“file”的文件中,我有这个“FFIEC CDR Call Schedule RCB02 03312011.txt”。如果我尝试运行您的代码,例如: df["file"].str.extract(r"FFIEC CDR Call Schedule (\w+) \d+\.txt").values() 我收到此错误消息: TypeError : 'numpy.ndarray' 对象不可调用
    • 原df上的索引是这样的:df["file"].str.extract(r"FFIEC CDR Call Schedule (\w+) \d+\.txt").values () 对。我认为应该是。我现在很困惑。您的代码在您的示例中运行良好,但在我的实际数据集中却不行。
    • 没有看到所有df 的样本,我只是按照你所说的去做。您也可以在表达式上尝试 .locdf["schedule_code"] = df.loc[:, "file"].str.extract(r"FFIEC CDR Call Schedule (\w+) \d+\.txt")。这对我有用。
    猜你喜欢
    • 2019-11-06
    • 2013-07-01
    • 1970-01-01
    • 2021-04-30
    • 2019-11-10
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    相关资源
    最近更新 更多