我试图在 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)
a
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
任何有关防止此问题的帮助将不胜感激。
慕田峪9158850
相关分类