np.数组平均到单列数据帧

2024-04-20 15:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个2列数组,我计算平均值(从而创建列a)。我希望能够引用和操作列A,但似乎无法将其另存为新的单个列。下面是我的一个具体例子,“filtered”是我希望能够保存/使用的/错误是有规律的ValueError: Wrong number of items passed 2, placement implies 1。你知道吗

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df=pd.read_csv('/Users/myfile.csv', delimiter=',', usecols=['Time','Distance'])

x = df['Time']
y = df['Distance']

n = 25  #small n = less smoothed

fwd = pd.Series.ewm(df,span=n, adjust=True).mean()
bwd = pd.Series.ewm(df[::-1],span=n, adjust=True).mean()
filtered = np.stack(( fwd, bwd[::-1] ))
filtered2 = np.mean(filtered, axis=0)
plt.subplot(2,1,1)
plt.title('smoothed and raw data')
plt.plot(x,y, color = 'orange')
plt.plot(x,filtered, color='green')
plt.plot(x,fwd, color='red')
plt.plot(x[::-1],bwd, color='blue')
plt.xlabel('time')
plt.ylabel('distance')

df['filtered2'] = pd.DataFrame(filtered, dtype='str', index=None)
print(filtered2)

smoothed_velocity = ((df.filtered2 - df.filtered2.shift(1)) /  df['Time'] - df['Time'].shift(1))

print(smoothed_velocity)
plt.subplot (2,1,2)
plt.title ('smoothed velocity')
plt.plot (smoothed_velocity, color = 'orange')

plt.tight_layout()
plt.show()

因为我定义了“filter”两次,所以我尝试将一个变量更改为另一个变量,但没有成功。出现的错误是ValueError: x and y must have same first dimension, but have shapes (458,) and (2, 458, 2)

任何帮助都是不可能的!你知道吗


Tags: importdftimeplotasnppltmean