【问题标题】:Convert a column of data type Int64 with <NA> values to object with nan values将具有 <NA> 值的数据类型 Int64 的列转换为具有 nan 值的对象
【发布时间】:2020-12-06 12:33:20
【问题描述】:

一个教程有这个数据框sequels如下:

              title sequel
id                        
19995        Avatar    nan
862       Toy Story    863
863     Toy Story 2  10193
597         Titanic    nan
24428  The Avengers    nan

<class 'pandas.core.frame.DataFrame'>
Index: 4803 entries, 19995 to 185567
Data columns (total 2 columns):
title     4803 non-null object
sequel    4803 non-null object
dtypes: object(2)
memory usage: 272.6+ KB

教程提供了一个文件sequels.p。但是,当我读入文件时,我的数据框与教程中的不同

my_sequels = pd.read_pickle('data/pandas/sequels.p')
my_sequels.set_index('id', inplace=True)
my_sequels.head()
             title  sequel
id      
19995       Avatar  <NA>
862      Toy Story  863
863    Toy Story 2  10193
597        Titanic  <NA>
24428  The Avengers <NA>

sequels.info()
<class 'pandas.core.frame.DataFrame'>
Index: 4803 entries, 19995 to 185567
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   title   4803 non-null   object
 1   sequel  90 non-null     Int64 
dtypes: Int64(1), object(1)
memory usage: 117.3+ KB

我的问题是:有没有办法将my_sequels 操作为类似于sequels,也就是说,将my_sequels['sequel'] 作为一个4803 非空的对象,其中&lt;NA&gt; 变为nan

编辑:我想让my_sequelssequels 相同的原因是为了避免后续步骤中的错误:

sequels_fin = my_sequels.merge(financials, on='id', how='left')

orig_seq = sequels_fin.merge(sequels_fin, how='inner', left_on='sequel', 
                             right_on='id', right_index=True,
                             suffixes=('_org','_seq'))

ValueError                                Traceback (most recent call last)
<ipython-input-5-7215de303684> in <module>
      3 orig_seq = sequels_fin.merge(sequels_fin, how='inner', left_on='sequel', 
      4                              right_on='id', right_index=True,
----> 5                              suffixes=('_org','_seq'))
ValueError: cannot convert to 'int64'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.

【问题讨论】:

  • 如果你愿意,你可以使用df['sequel'].astype(str),但实际上这就像倒退。

标签: python pandas pickle


【解决方案1】:

我想你不会想要的。您看到此内容的原因是本教程基于旧版本的 Pandas,而不是您使用的版本。

https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html

您可以按照您的预期检测和处理缺失值。

arr = pd.array([1, 2, None], dtype=pd.Int64Dtype())
arr.isna()
array([False, False,  True])
arr.fillna(0)
<IntegerArray>
[1, 2, 0]
Length: 3, dtype: Int64

【讨论】:

    【解决方案2】:

    第一个索引“id”:

    sequels_fin = sequels_fin.set_index('id')
    

    之后:

    orig_seq = sequels_fin.merge(sequels_fin, how='inner', left_on='sequel', 
                                 right_on='id', right_index=True,
                                 suffixes=('_org','_seq'))
    

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 2012-06-11
      • 2022-01-07
      相关资源
      最近更新 更多