在通过代码连接的两个表之间使用更新时出错

2023-12-10

是以下 python pandas DataFrame:

ID country money code money_add other time
832932 Other NaN 00000 NaN [N2,N2,N4] 0 days 01:37:00
217#8# NaN NaN NaN NaN [N1,N2,N3] 2 days 01:01:00
1329T2 France 12131 00020 3452 [N1,N1] 1 days 03:55:00
124932 France NaN 00016 NaN [N2] 0 days 01:28:00
194022 France NaN 00000 NaN [N4,N3] 3 days 02:35:00

If code列不是NaNmoney列是NaN,我们更新值money and money_add从下表中。使用code and cod_t列作为键。

cod_t money money_add
00000 4532 72323
00016 1213 23822
00030 1313 8393
00020 1813 27328

结果表的示例:

ID country money code money_add other time
832932 Other 4532 00000 72323 [N2,N2,N4] 0 days 01:37:00
217#8# NaN NaN NaN NaN [N1,N2,N3] 2 days 01:01:00
1329T2 France 12131 00020 3452 [N1,N1] 1 days 03:55:00
124932 France 1213 00016 23822 [N2] 0 days 01:28:00
194022 France 4532 00000 72323 [N4,N3] 3 days 02:35:00

用户@jezrael,给了我以下问题的解决方案:

df1 = df1.drop_duplicates('cod_t').set_index('cod_t')
df = df.set_index(df['code'])
df.update(df1, overwrite=False)
df = df.reset_index(drop=True).reindex(df.columns, axis=1)

但这段代码给了我一个我不知道如何解决的错误:

TypeError: The DType <class 'numpy.dtype[timedelta64]'> could not be promoted by <class
'numpy.dtype[float64]'>. This means that no common DType exists for the given inputs. 
For example they cannot be stored in a single array unless the dtype is `object`. 
The full list of DTypes is: (<class 'numpy.dtype[timedelta64]'>, <class 'numpy.dtype[float64]'>)
// First DataFrame dtypes
ID                                 object
country                            object
code                               object
money                             float64
money_add                         float64
other                              object
time                      timedelta64[ns]
dtype: object
// Second DataFrame dtypes
cod_t                     object
money                      int64
money_add                  int64
dtype: object

如果您能帮助我解决错误,或者建议使用替代方法,我将不胜感激update.


Because DataFrame.update这里效果不好是替代方案 - 首先对第二个 DataFrame 中的新列使用左连接DataFrame.merge:

df2 = df.merge(df1.drop_duplicates('cod_t').rename(columns={'cod_t':'code'}), 
               on='code', 
               how='left',
               suffixes=('','_'))

print (df2)
       ID country    money  code  money_add         other            time  \
0  832932   Other      NaN   0.0        NaN  [N2, N2, N4] 0 days 01:37:00   
1  217#8#     NaN      NaN   NaN        NaN  [N1, N2, N3] 2 days 01:01:00   
2  1329T2  France  12131.0  20.0     3452.0      [N1, N1] 1 days 03:55:00   
3  124932  France      NaN  16.0        NaN          [N2] 0 days 01:28:00   
4  194022  France      NaN   0.0        NaN      [N4, N3] 3 days 02:35:00   

   money_  money_add_  
0  4532.0     72323.0  
1     NaN         NaN  
2  1813.0     27328.0  
3  1213.0     23822.0  
4  4532.0     72323.0 

然后获取带/不带的列名称_:

cols_with_ = df2.columns[df2.columns.str.endswith('_')]
cols_without_ = cols_with_.str.rstrip('_')

print (cols_with_)
Index(['money_', 'money_add_'], dtype='object')

print (cols_without_)
Index(['money', 'money_add'], dtype='object')

Pass to DataFrame.combine_first最后删除辅助列:

df2[cols_without_] = (df2[cols_without_].combine_first(df2[cols_with_]
                                        .rename(columns=lambda x: x.rstrip('_'))))
df2 = df2.drop(cols_with_, axis=1)
print (df2)
       ID country    money  code  money_add         other            time
0  832932   Other   4532.0   0.0    72323.0  [N2, N2, N4] 0 days 01:37:00
1  217#8#     NaN      NaN   NaN        NaN  [N1, N2, N3] 2 days 01:01:00
2  1329T2  France  12131.0  20.0     3452.0      [N1, N1] 1 days 03:55:00
3  124932  France   1213.0  16.0    23822.0          [N2] 0 days 01:28:00
4  194022  France   4532.0   0.0    72323.0      [N4, N3] 3 days 02:35:00
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在通过代码连接的两个表之间使用更新时出错 的相关文章

随机推荐

  • 将 mvc 4 降级/转换为 mvc 3

    我知道这是一个相当奇怪的问题 请不要攻击我 我具体需要将当前的 Asp Net Mvc 4 net Framework 4 5 应用程序 降级 到 Asp Net Mvc 3 net Framework 4 0 因为我们的共享托管提供商不支
  • 将通过URL获取的数据转换为post变量

    我需要读取使用 POST 操作类型提交的表单的结果 那么 我是否可以将通过 GET 变量获得的变量转换为 POST 然后我可以使用 file get contents 简单地读取内容 请帮助我使用此方法或通过某种替代方法 如果可能 获取数据
  • 我应该为 jar commons-lang.jar 使用哪个 pom 依赖项

    如果 jar 名称中没有版本 我如何知道应该使用哪个版本的 pom 依赖项 例如 jar commons lang jar 我应该使用什么版本的 pom 依赖项 以下是其在 Maven 中央存储库上的搜索结果 http search mav
  • 查找字符串中出现频率最高的单词并检查字符串是否仅包含 [a-z][A-Z] 个字符

    我用 Python 创建了一些代码来查找字符串中出现频率最高的单词 我对 Python 还很陌生 请求你的帮助 看看我是否可以更好 更有效地编写这个代码 代码返回字符串中出现频率最高的单词的一个整数 另外我想确保该字符串仅包含 a z A
  • 使用 celery 运行“独特”任务

    我使用 celery 来更新我的新闻聚合网站中的 RSS 提要 我为每个提要使用一个 task 并且一切似乎运行良好 有一个细节我不确定处理得很好 所有提要都使用 periodic task 每分钟更新一次 但是如果在启动新任务时提要仍在从
  • 溢出:隐藏在 div 标签上会影响背景颜色

    的定义overflow hidden指出 the overflowing content is completely hidden not accessible to the user from http quirksmode org cs
  • 使用 import java.* 来使用“java”包下的所有子包怎么样?

    import java 为什么我不能进行此导入 我没有导入 java 包的特定子包中的所有类 而是尝试导入 java 包下的所有子包 如何导入所有内置类 java中没有子包这种东西 java util stream不是 的子包java ut
  • STRCHR 与 STRRCHR 有何区别? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心以获得指导 我想知道这两种不同用途之间
  • 通过RemoteView设置GradientDrawable

    这就是我想要做的 我有一个小部件 我想根据用户选择的颜色设置其背景 它必须是一个渐变 背景是通过设置 LinearLayout 的背景来设置的 为了进行测试 我对虚拟背景进行了如下操作 remoteViews setInt R id lay
  • WPF DataBound ListBox 添加动画但不滚动

    我有一个绑定到 ObservableCollection 的 WPF ListBox 当我向其中添加项目时 我想要一些动画来 吸引 新来者 的注意力 有许多使用 DataTemplate 触发器和 FrameworkElment Loade
  • 如何为 Telerik RadGrid 中的列标题指定颜色

    我在 asp net 中创建了一个 telerik RadGrid 我的要求是为列标题提供我自己的颜色 我们怎样才能实现这一点 下面是我正在使用的代码结构
  • Seahorse::Client::NetworkingError 使用 Rails 上传 Amazon S3 文件

    在我的 Rails 4 应用程序中 我尝试使用 aws sdk 下载常规 png 文件 然后将其上传到我的 s3 存储桶 使用gem aws sdk gt 2 在开发环境中 代码运行完全正常 但如果我尝试rails s e producti
  • IBM MQ 客户端在 10 分钟后断开连接:IBM.XMS.IllegalStateException

    我正在使用 IBM 的这个示例 我刚刚复制并粘贴了代码 https github com ibm messaging mq dev patterns blob master dotnet dotNetGet cs 我正在连接到 MQ 服务器
  • 如何初始化参数化构造函数的对象数组

    我需要初始化参数化构造函数的对象数组 我怎样才能以最好的方式做到这一点 include
  • 当您使用 Seq(1,2,3) 创建 Seq 对象时会发生什么?

    当您评估表达式时到底会发生什么 Seq 1 2 3 我是 Scala 新手 现在对各种集合类型有点困惑 Seq是一种特质 对吗 所以当你这样称呼它时 Seq 1 2 3 它一定是某种伴生对象吗 或不 它是某种扩展的类吗Seq 最重要的是 返
  • 在 Xamarin 应用程序中使用 Identity Server

    我正在尝试让 Xamarin 应用程序与身份服务器一起使用 我已按照以下步骤操作 1 下载这个 https github com IdentityModel IdentityModel OidcClient Samples tree mas
  • 为什么这段显示图像的代码在构建到 jar 中时会出现“错误”?

    我想通过在 JLabel 上绘制 BufferedImage 来显示图像 x y Offset是在JLabel的中间绘制一个较小的图像 如果我在 IDE 中运行代码 它会正常工作并在我的 JFrame 上显示图像 如果我现在将类构建到 ja
  • 使用 Unicode emoji 动态创建 NSString

    我有字符串 Hi there U0001F603 它正确显示了表情符号 例如Hi there 如果我把它放在UILabel 但我想像这样动态创建它 NSString stringWithFormat Hi there U0001F60 ld
  • 了解分支预测

    有一些关于分支预测的疑问我无法自信地弄清楚 假设我必须使用静态分支预测器 分支预测应该发生在管道的哪个阶段 如何知道预测出错了 数据路径如何知道发生了错误预测 如果它知道发生了错误预测 它如何发送信号以占用未占用的分支 出了问题后 我必须占
  • 在通过代码连接的两个表之间使用更新时出错

    是以下 python pandas DataFrame ID country money code money add other time 832932 Other NaN 00000 NaN N2 N2 N4 0 days 01 37