【问题标题】:Data Frame Column Copying resulting in Nan数据框列复制导致 Nan
【发布时间】:2021-02-15 16:53:14
【问题描述】:

我正在尝试将数据从一个 Panadas DataFrame 复制到另一个中,但得到了一些奇怪的结果。例如,如果我有:

[In]:
A = {'Types':['Falcon', 'Eagle', 'sparrow'], 
     'Speed':[100, 75, 50]}
df_A = pd.DataFrame(A)

df_B = pd.DataFrame()
df_B['Type'] = df_A['Types']
df_B['tags'] = ['FLCN', 'EGLE', 'SPRW']
df_B['ID'] = [543.76, 534.32, 645.25]

df_A['Tags'] = df_B['tags']
df_A['ID'] = df_B['ID']
df_A

我期望得到的是:

[Out]:
    Types   Speed   Tags    ID
0   Falcon  100     FLCN    543.76
1   Eagle   75      EGLE    534.32
2   sparrow 50      SPRW    645.25

但我得到的是:

[Out]:
    Types   Speed   Tags    ID
0   Falcon  100     FLCN    NaN
1   Eagle   75      EGLE    NaN
2   sparrow 50      SPRW    NaN

我已尝试在 Jupyter Notebook 中执行此操作以排除故障并收到TypeError: "'Method' object is not subscriptable"。这是我收到的类型错误的示例:

例如。 2:

[In]:
df_A['ID'] = df_B['ID']

[Out]:
TypeError: 'method' object is not subscriptable

一旦我决定写一个问题,我就在 Jupyter 中为这些示例编写了代码,并且没有任何问题得到了预期的结果,所以我很难过。

编辑添加: 我尝试使用以下解决方法:

[In]:
df_A['Tags'] = df_B['tags']
df_A = pd.concat(df_A, df_B['ID'], axis = 1)

但我仍然得到时髦的结果。有了这个示例代码,我最终得到:

[Out]:
    Types   Speed   Tags    ID
0   Falcon  100     FLCN    543.76
1   Eagle   75      EGLE    534.32
2   sparrow 50      SPRW    645.25

但当我使用更大的数据集时,结果如下所示:

[Out]:
    Types   Speed   Tags    ID
0   NaN     NaN     NaN     543.76
1   NaN     NaN     NaN     534.32
2   NaN     NaN     NaN     645.25
3   Falcon  100     FLCN    NaN
4   Eagle   75      EGLE    NaN
5   sparrow 50      SPRW    NaN

尽管在 pd.concat 中使用 'axis=1' 作为参数。

【问题讨论】:

  • 这对我有用,无法复制。
  • @Erfan 感谢您的尝试,我自己无法重现它。它只发生在我的实际数据集的一个地方。我复制了多列,但只有一列有这个问题
  • 在您的解决方法中添加ignore_index=True pd.concat([df_A, df_B['tags']], axis=1, ignore_index=True)
  • @viniciusrf1992 谢谢!在我的代码中的某个时刻,我有df_A.index = df_B['Name'],当我尝试在原始行的代码中添加它时它不起作用 - 但是,我预感在设置索引之前将pd.concat移动到并且能够得到我想要的结果。作为将来发现此问题的任何人的说明。我确实必须在 concat 之后重命名我的所有列,但它现在可以工作了。我可以编辑我的问题以包含它,但我想感谢你,所以如果你愿意,你可以更新你的答案,我可以选择它作为正确的答案。
  • 很高兴听到它有所帮助。

标签: python pandas dataframe nan


【解决方案1】:

在每一步,使用提供的代码,我也没有得到 NaN。

import pandas as pd


def print_df_A_df_B(stage, df_A, df_B):
    print(stage, "assignment","\ndf_A\n",df_A,"\ndf_B\n",df_B,"\n")
    pass

A = {'Types':['Falcon', 'Eagle', 'sparrow'], 
     'Speed':[100, 75, 50]}
df_A = pd.DataFrame(A)

df_B = pd.DataFrame()
df_B['Type'] = df_A['Types']
print_df_A_df_B("Type", df_A, df_B)
df_B['tags'] = ['FLCN', 'EGLE', 'SPRW']
df_B['ID'] = [543.76, 534.32, 645.25]

df_A['Tags'] = df_B['tags']
print_df_A_df_B("Tags", df_A, df_B)
df_A['ID'] = df_B['ID']
print_df_A_df_B("ID", df_A, df_B)

出来

Type assignment 
df_A
      Types  Speed
0   Falcon    100
1    Eagle     75
2  sparrow     50 
df_B
       Type
0   Falcon
1    Eagle
2  sparrow 

Tags assignment 
df_A
      Types  Speed  Tags
0   Falcon    100  FLCN
1    Eagle     75  EGLE
2  sparrow     50  SPRW 
df_B
       Type  tags      ID
0   Falcon  FLCN  543.76
1    Eagle  EGLE  534.32
2  sparrow  SPRW  645.25 

ID assignment 
df_A
      Types  Speed  Tags      ID
0   Falcon    100  FLCN  543.76
1    Eagle     75  EGLE  534.32
2  sparrow     50  SPRW  645.25 
df_B
       Type  tags      ID
0   Falcon  FLCN  543.76
1    Eagle  EGLE  534.32
2  sparrow  SPRW  645.25

也许尝试输入您的完整代码:print(type(df_A['ID']), type(df_B['ID'])) 以检查两者是否都是 pandas.core.series.Series

对于您报告的解决方法,也许将ignore_index=True 添加为pd.concat([df_A, df_B['tags']], axis=1, ignore_index=True),然后修复列名。

【讨论】:

  • 我刚刚检查过,它们都匹配:
【解决方案2】:

使用您的代码,我得到了这个(Jupiter Notebook)。所以你的代码似乎工作正常?

Types   Speed   Tags    ID
0   Falcon  100 FLCN    543.76
1   Eagle   75  EGLE    534.32
2   sparrow 50  SPRW    645.25

【讨论】:

  • 运行示例代码对我来说运行良好,但使用更大的代码我仍然得到 NaN 结果。代码的编写方式相同,只是使用了不同的 DataFrame。我编写示例是为了简化正在使用的数据并使问题更易于理解
猜你喜欢
  • 1970-01-01
  • 2020-04-23
  • 2019-12-01
  • 2019-09-15
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
相关资源
最近更新 更多