簡體   English   中英

為什么pandas.DataFrame.mean()可以工作,但pandas.DataFrame.std()不能覆蓋相同的數據

[英]Why does pandas.DataFrame.mean() work but pandas.DataFrame.std() does not over same data

我試圖弄清楚為什么pandas.DataFrame.mean()函數可以對ndarrays的ndarray起作用,但是pandas.DataFrame.std()不能對相同的數據起作用。 以下是最小示例。

x = np.array([1,2,3])
y = np.array([4,5,6])
df = pd.DataFrame({"numpy": [x,y]})

df["numpy"].mean() #works as expected
Out[231]: array([ 2.5,  3.5,  4.5])

df["numpy"].std() #does not work as expected
Out[231]: TypeError: setting an array element with a sequence.

但是,如果我通過

df["numpy"].values.mean() #works as expected
Out[231]: array([ 2.5,  3.5,  4.5])

df["numpy"].values.std() #works as expected
Out[233]: array([ 1.5,  1.5,  1.5])

調試信息:

df["numpy"].dtype
Out[235]: dtype('O')

df["numpy"][0].dtype
Out[236]: dtype('int32')

df["numpy"].describe()
Out[237]: 
count             2
unique            2
top       [1, 2, 3]
freq              1
Name: numpy, dtype: object

df["numpy"]
Out[238]: 
0    [1, 2, 3]
1    [4, 5, 6]
Name: numpy, dtype: object

假設您具有以下原始DF(在單元格中包含相同形狀的numpy數組):

In [320]: df
Out[320]:
  file      numpy
0    x  [1, 2, 3]
1    y  [4, 5, 6]

將其轉換為以下格式:

In [321]: d = pd.DataFrame(df['numpy'].values.tolist(), index=df['file'])

In [322]: d
Out[322]:
      0  1  2
file
x     1  2  3
y     4  5  6

現在,您可以自由使用所有的Pandas / Numpy / Scipy功能:

In [323]: d.sum(axis=1)
Out[323]:
file
x     6
y    15
dtype: int64

In [324]: d.sum(axis=0)
Out[324]:
0    5
1    7
2    9
dtype: int64

In [325]: d.mean(axis=0)
Out[325]:
0    2.5
1    3.5
2    4.5
dtype: float64

In [327]: d.std(axis=0)
Out[327]:
0    2.12132
1    2.12132
2    2.12132
dtype: float64

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM