将 Pandas 的所有列乘以DataFrame
存储在 a 中的列向量的最佳方法是Series
什么?我曾经在 Matlab 中使用repmat()
,这在 Pandas 中不存在。我可以使用np.tile()
,但是每次来回转换数据结构看起来很难看。
谢谢。
将 Pandas 的所有列乘以DataFrame
存储在 a 中的列向量的最佳方法是Series
什么?我曾经在 Matlab 中使用repmat()
,这在 Pandas 中不存在。我可以使用np.tile()
,但是每次来回转换数据结构看起来很难看。
谢谢。
有什么问题
result = dataframe.mul(series, axis=0)
?
这可以通过 DataFrame 方法非常简单地完成apply
。
In[1]: import pandas as pd; import numpy as np
In[2]: df = pd.DataFrame(np.arange(40.).reshape((8, 5)), columns=list('abcde')); df
Out[2]:
a b c d e
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
4 20 21 22 23 24
5 25 26 27 28 29
6 30 31 32 33 34
7 35 36 37 38 39
In[3]: ser = pd.Series(np.arange(8) * 10); ser
Out[3]:
0 0
1 10
2 20
3 30
4 40
5 50
6 60
7 70
现在我们有了我们的DataFrame
并且Series
我们需要一个函数来传递给apply
.
In[4]: func = lambda x: np.asarray(x) * np.asarray(ser)
我们可以把它传给df.apply
我们,我们很高兴
In[5]: df.apply(func)
Out[5]:
a b c d e
0 0 0 0 0 0
1 50 60 70 80 90
2 200 220 240 260 280
3 450 480 510 540 570
4 800 840 880 920 960
5 1250 1300 1350 1400 1450
6 1800 1860 1920 1980 2040
7 2450 2520 2590 2660 2730
df.apply
默认情况下按列执行,但它也可以通过axis=1
作为参数传递给apply
.
In[6]: ser2 = pd.Series(np.arange(5) *5); ser2
Out[6]:
0 0
1 5
2 10
3 15
4 20
In[7]: func2 = lambda x: np.asarray(x) * np.asarray(ser2)
In[8]: df.apply(func2, axis=1)
Out[8]:
a b c d e
0 0 5 20 45 80
1 0 30 70 120 180
2 0 55 120 195 280
3 0 80 170 270 380
4 0 105 220 345 480
5 0 130 270 420 580
6 0 155 320 495 680
7 0 180 370 570 780
这可以通过在内部定义匿名函数来更简洁地完成apply
In[9]: df.apply(lambda x: np.asarray(x) * np.asarray(ser))
Out[9]:
a b c d e
0 0 0 0 0 0
1 50 60 70 80 90
2 200 220 240 260 280
3 450 480 510 540 570
4 800 840 880 920 960
5 1250 1300 1350 1400 1450
6 1800 1860 1920 1980 2040
7 2450 2520 2590 2660 2730
In[10]: df.apply(lambda x: np.asarray(x) * np.asarray(ser2), axis=1)
Out[10]:
a b c d e
0 0 5 20 45 80
1 0 30 70 120 180
2 0 55 120 195 280
3 0 80 170 270 380
4 0 105 220 345 480
5 0 130 270 420 580
6 0 155 320 495 680
7 0 180 370 570 780
为什么不创建自己的数据框平铺功能:
def tile_df(df, n, m):
dfn = df.T
for _ in range(1, m):
dfn = dfn.append(df.T, ignore_index=True)
dfm = dfn.T
for _ in range(1, n):
dfm = dfm.append(dfn.T, ignore_index=True)
return dfm
df = pandas.DataFrame([[1,2],[3,4]])
tile_df(df, 2, 3)
# 0 1 2 3 4 5
# 0 1 2 1 2 1 2
# 1 3 4 3 4 3 4
# 2 1 2 1 2 1 2
# 3 3 4 3 4 3 4
但是,文档指出:“DataFrame 并非旨在替代 ndarray,因为它的索引语义在某些地方与矩阵完全不同。” 这大概应该被解释为“如果你正在做很多矩阵的东西,请使用 numpy”。