我有一个csv文件,我正在阅读,清理和分析大熊猫。我选择相关数据,然后为每列创建一个均值列表(然后我将其用作新数据帧的新数据)。一切似乎都有效 - 然而,当我根据平均值/平均值"手动"重复检查数据时。在Excel中计算,pandas和Excel值不同。
我可以在此处找到我正在使用的csv文件:https://drive.google.com/open?id=1TPczQoh1oS-RaRpepd4evxM919699Dss。原始文件来自https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/aberporthdata.txt;第一个链接只是清理和准备好的版本。
months = [3,4,5]
l = []
for j, station in enumerate(stations):
df = pd.read_csv('/Users/Ji/Documents/' + station + 'data_clean.csv')
df = df.drop('empty', axis=1).replace('---', np.nan)
df = df.loc[df['mm'].isin(months)]
df['station'] = station
df = df.astype({'mm': np.int32,'tmax': np.float32,'tmin': np.float32,'af': np.float32,'rain': np.float32,'sun': np.float32, 'station': np.str})
df = df.drop(['mm','yyyy'], axis=1)
row = [0]*6
for i, col in enumerate(list(df)):
if col == 'station':
row[5] = station
continue
row[i] = df[col].mean(skipna=True)
l.insert(j, row)
df_means = pd.DataFrame(data=l, columns=list(df))
我在熊猫中获得这个特定文件的方法是:
tmax tmin af rain sun station
0 7.582970 3.190000 4.924325 84.921890 61.074783 aberporth
我在Excel中得到的平均值是:
tmax tmin af rain sun
12.38645949 7.193654267 1.576294278 75.78479784 129.2139254
我很感激任何想法或解释,为什么会这样,以及如何解决它!
答案 0 :(得分:0)
这会输出我从Excel获得的相同值。
import pandas as pd
import numpy as np
df = pd.read_csv('C:\orig.csv')
df = df.drop('empty', axis=1)
df.replace('---', np.nan, inplace=True)
for col in df.columns:
if df[col].dtype == 'object':
df[col] = pd.to_numeric(df[col], downcast='float')
mean_vals = df.mean()
mean_vals
Out[44]:
yyyy 1979.209903
mm 6.481163
tmax 12.411418
tmin 7.188440
af 1.578019
rain 75.767384
sun 129.306442
dtype: float64
# Output from Excel
1979.21 6.48 12.41 7.19 1.58 75.77 129.31