【问题标题】:getting error: TypeError: object of type 'float' has no len() in pandas出现错误:TypeError:'float' 类型的对象在 pandas 中没有 len()
【发布时间】:2019-10-10 08:26:53
【问题描述】:

我有一个熊猫数据框df

import numpy as np
import pandas as pd

df = pd.DataFrame({"ID": [2,3,4,5,6,7,8,9,10],
      "type" :["A", "B", "B", "A", "A", "B", "A", "A", "A"],
      "F_ID" :["0", "[7 8 9]", "[10]", "0", "[2]", "0", "0", "0", "0"]})

# convert the string representations of list structures to actual lists
F_ID_as_series_of_lists = df["F_ID"].str.replace("[","").str.replace("]","").str.split(" ")

#type(F_ID_as_series_of_lists) is pd.Series, make it a list for pd.DataFrame.from_records
F_ID_as_records = list(F_ID_as_series_of_lists)

f_id_df = pd.DataFrame.from_records(list(F_ID_as_records)).fillna(np.nan)

我在该行中遇到错误:

f_id_df = pd.DataFrame.from_records(list(F_ID_as_records)).fillna(np.nan)

错误是:TypeError: object of type 'float' has no len()

我该如何解决这个问题?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    还有另一种使用列表推导式并利用我们从类型错误本身中学到的知识的方法。

    假设您有一个字符串数据类型的 pandas 系列,并且您希望在给定“/”符号的情况下将该列拆分为两部分,但并非所有列都已填充。

    pd.DataFrame({'TEXT_COLUMN' : ['12/4', '54/19', np.NaN, '89/33']})
    

    ..我们想将该列分成两个不同的列,但是我们知道当我们将它放回 DataFrame 时,pandas 会搞砸,所以让我们把它放在一个列表中:

    split_list = list(df.TEXT_COLUMN.str.split('/'))
    

    split_list 返回,我们可以看到为什么在尝试解析时会出现浮点错误:

    >> [['12','4'],['54','19'], np.NaN, ['89','33']]
    

    现在我们有了这个列表,我们想把它放在一个可以纠正空值问题的理解中。我们可以通过在推导式中创建一个条件类型来做到这一点:

    better_split_list = [x if type(x) != np.float else [None,None] for x in split_list]
    

    better_split_list 返回:

    >> [['12','4'],['54','19'], [None,None], ['89','33']]
    

    这使我们可以将列表放入一个自己的 pandas DataFrame 中,并以更健壮的方式分隔列:

    pd.DataFrame(better_split_list, columns = ['VALUE_1','VALUE_2'])
    

    【讨论】:

      【解决方案2】:

      问题显然是一些NoneNaN 值,但如果使用str.split 和参数expand=True 来获得新的DataFrame,它会正确处理。

      也可以使用replace 代替str.strip

      df = pd.DataFrame({"ID": [2,3,4,5,6,7,8,9,10],
            "type" :["A", "B", "B", "A", "A", "B", "A", "A", "A"],
            "F_ID" :[None, "[7 8 9]", "[10]", np.nan, "[2]", "0", "0", "0", "0"]})
      
      print (df)
         ID type     F_ID
      0   2    A     None
      1   3    B  [7 8 9]
      2   4    B     [10]
      3   5    A      NaN
      4   6    A      [2]
      5   7    B        0
      6   8    A        0
      7   9    A        0
      8  10    A        0
      

      f_id_df = df["F_ID"].str.strip("[]").str.split(expand=True)
      print (f_id_df)
            0     1     2
      0  None  None  None
      1     7     8     9
      2    10  None  None
      3   NaN   NaN   NaN
      4     2  None  None
      5     0  None  None
      6     0  None  None
      7     0  None  None
      8     0  None  None
      

      如果需要,最后将值转换为数字:

      f_id_df = df["F_ID"].str.strip("[]").str.split(expand=True).astype(float)
      print (f_id_df)
            0    1    2
      0   NaN  NaN  NaN
      1   7.0  8.0  9.0
      2  10.0  NaN  NaN
      3   NaN  NaN  NaN
      4   2.0  NaN  NaN
      5   0.0  NaN  NaN
      6   0.0  NaN  NaN
      7   0.0  NaN  NaN
      8   0.0  NaN  NaN
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-15
        • 2021-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多