猿问

将处理过的 Pandas DataFrames 添加在一起

我试图在 Python 中将两个 DataFrame 添加在一起,首先将它们的索引列设置为等于现有列之一。

但是,在以下线程中使用评分最高的方法会出现错误:

这是问题的一个简单示例:


import pandas as pd

import numpy as np


a = np.array([['A',1.,2.,3.],['B',1.,2.,3.],['C',1.,2.,3.]])

a = pd.DataFrame(a)

a = a.set_index(0)



     1    2    3

0               

A  1.0  2.0  3.0

B  1.0  2.0  3.0

C  1.0  2.0  3.0


b = np.array([['A',1.,2.,3.],['B',1.,2.,3.]])

b = pd.DataFrame(b)

b.set_index(0)


b


     1    2    3

0               

A  1.0  2.0  3.0

B  1.0  2.0  3.0


df_add = a.add(b,fill_value=1)

和错误:


Traceback (most recent call last):


  File "<ipython-input-150-885d92411f6c>", line 1, in <module>

    df_add = a.add(b,fill_value=1)


  File "/home/anaconda3/lib/python3.6/site-packages/pandas/core/ops.py", line 1234, in f

    return self._combine_frame(other, na_op, fill_value, level)


  File "/home/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3490, in _combine_frame

    result = _arith_op(this.values, other.values)


  File "/home/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3459, in _arith_op

    return func(left, right)


  File "/home/anaconda3/lib/python3.6/site-packages/pandas/core/ops.py", line 1195, in na_op

    result[mask] = op(xrav, yrav)


TypeError: must be str, not int

任何有关防止此问题的帮助将不胜感激。


30秒到达战场
浏览 138回答 1
1回答

慕田峪9158850

问题出在已定义的 DataFrame 中 - 所有数据都转换为 2d numpy 数组中的字符串:a = np.array([['A',1.,2.,3.],['B',1.,2.,3.],['C',1.,2.,3.]])print (a)[['A' '1.0' '2.0' '3.0']&nbsp;['B' '1.0' '2.0' '3.0']&nbsp;['C' '1.0' '2.0' '3.0']]解决方案是删除字符串值并按列表指定索引:a = np.array([[1.,2.,3.],[1.,2.,3.],[1.,2.,3.]])a = pd.DataFrame(a, index=list('ABC'))b = np.array([[1.,2.,3.],[1.,2.,3.]])b = pd.DataFrame(b, index=list('AB'))df_add = a.add(b,fill_value=1)print (df_add)&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 1&nbsp; &nbsp; 2A&nbsp; 2.0&nbsp; 4.0&nbsp; 6.0B&nbsp; 2.0&nbsp; 4.0&nbsp; 6.0C&nbsp; 2.0&nbsp; 3.0&nbsp; 4.0或者在将 index 设置为floats后转换 DataFrames :a = np.array([['A',1.,2.,3.],['B',1.,2.,3.],['C',1.,2.,3.]])a = pd.DataFrame(a)a = a.set_index(0).astype(float)b = np.array([['A',1.,2.,3.],['B',1.,2.,3.]])b = pd.DataFrame(b)b = b.set_index(0).astype(float)df_add = a.add(b,fill_value=1)print (df_add)&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; 2&nbsp; &nbsp; 30&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A&nbsp; 2.0&nbsp; 4.0&nbsp; 6.0B&nbsp; 2.0&nbsp; 4.0&nbsp; 6.0C&nbsp; 2.0&nbsp; 3.0&nbsp; 4.0
随时随地看视频慕课网APP

相关分类

Python
我要回答