我有以下两个数据框,我想乘以,但我没有做到这一点。任何帮助,将不胜感激。

第一个数据帧:

created_at    2019-06-28    2019-06-29    2019-06-30    2019-07-01
currency
1WO         2.600000e+05  2.600000e+05  2.604320e+05  2.604320e+05
ABX         1.033400e+04  1.033400e+04  1.033400e+04  1.033400e+04
ADH         8.219730e+05  8.219730e+05  8.219730e+05  8.219730e+05
ALX         2.500000e+04  2.500000e+04  2.500000e+04  2.500000e+04
AMLT        4.874240e+05  7.510590e+05  7.510600e+05  7.510600e+05
BCH         2.690000e+02  2.360000e+02  2.390000e+02 -3.982000e+03


第二个数据帧:

          2019-06-28    2019-06-29    2019-06-30    2019-07-01
currency
1WO       5.108000e+00  5.533000e+00  5.305000e+00  4.745000e+00
ADH       1.600000e-01  1.807000e-01  1.587000e-01  1.470000e-01
ALX       3.877000e-01  4.564000e-01  4.751000e-01  3.773000e-01
AMLT      6.078000e-01  1.051000e+00  6.900000e-01  4.087000e-01
BCH       4.537659e+04  4.728048e+04  4.786292e+04  4.355922e+04


第一个数据框信息:

<class 'pandas.core.frame.DataFrame'>
Index: 123 entries, 1WO to ZPR
Data columns (total 4 columns):
2019-06-28    123 non-null float64
2019-06-29    123 non-null float64
2019-06-30    123 non-null float64
2019-07-01    123 non-null float64
dtypes: float64(4)
memory usage: 4.8+ KB
None


第二个数据框信息:

<class 'pandas.core.frame.DataFrame'>
Index: 107 entries, 1WO to ZPR
Data columns (total 4 columns):
(2019-06-28 00:00:00,)    107 non-null float64
(2019-06-29 00:00:00,)    107 non-null float64
(2019-06-30 00:00:00,)    107 non-null float64
(2019-07-01 00:00:00,)    107 non-null float64
 dtypes: float64(4)
 memory usage: 4.2+ KB
 None


我的代码:

jpy_bal = pos_bal.mul(d_price, axis= 0)


错误:

File "inv_pos.py", line 321, in <module>
jpy_bal = pos_bal.mul(d_price, axis= 0)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/ops.py", line 1550, in f
return self._combine_frame(other, na_op, fill_value, level)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 4727, in _combine_frame
this, other = self.align(other, join='outer', level=level, copy=False)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 3550, in align
broadcast_axis=broadcast_axis)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 7367, in align
fill_axis=fill_axis)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 7394, in _align_frame
other.columns, how=join, level=level, return_indexers=True)
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3735, in join
return_indexers=return_indexers)
 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3830, in _join_multi
raise ValueError("cannot join with no level specified and no "
ValueError: cannot join with no level specified and no overlapping names

最佳答案

好吧,我从您的信息中得知,您的第二个df2列类型是datetime,并且是连接的多个索引,是单列,但是具有多个索引类型,所以我们进行转换,然后mul

df2.columns=df.columns.get_level_values(0)

df1.columns=pd.to_datetime(df1.columns)
df2.columns=pd.to_datetime(df2.columns)
df1.mul(df2)
            2019-06-28    2019-06-29    2019-06-30    2019-07-01
currency
1WO       1.328080e+06  1.438580e+06  1.381592e+06  1.235750e+06
ABX                NaN           NaN           NaN           NaN
ADH       1.315157e+05  1.485305e+05  1.304471e+05  1.208300e+05
ALX       9.692500e+03  1.141000e+04  1.187750e+04  9.432500e+03
AMLT      2.962563e+05  7.893630e+05  5.182314e+05  3.069582e+05
BCH       1.220630e+07  1.115819e+07  1.143924e+07 -1.734528e+08

关于python - 在2个数据帧之间相乘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56829390/

10-09 18:51