【问题标题】:Obtaining the mean value of a DataFrame获取 DataFrame 的平均值
【发布时间】:2018-07-11 09:27:07
【问题描述】:

我正在尝试使用 Pandas 模块获取 DataFrame 中每一列的平均值。该图像显示了我正在使用的数据的示例。

只有索引“3 to end”与计算相关。

我尝试使用此处给出的示例: how to get the average of dataframe column values

但是,结果我得到:

In: concatenated_df.mean(axis=0)
Out: Series([], dtype: float64)

谁能解释一下这个情况?

提前致谢!

concatenated_df

我连接文件的代码:

# appending the signal column of every csv file into a single matrix
import pandas as pd
import glob
import os


path =r'mypath' # use your path
all_files = glob.glob(os.path.join(path, "*.csv"))


df_from_each_file = (pd.read_csv(f,delim_whitespace=0,  usecols=[1]) for f in all_files)
concatenated_df   = pd.concat(df_from_each_file, axis=1)

concatenated_df

# save concatenated_df to .csv
concatenated_df.to_csv

【问题讨论】:

  • @juanpa.arrivillaga:我提供了连接文件的代码。在图像 'concatenated_df'.. 你可以看到输出。我希望从每一列中获得平均值。我可以提供更多信息吗?
  • 那不是minimal reproducible example。请阅读该链接,也许还有How to Ask。不妨看看help center
  • @juanpa.arrivillaga:我一定会仔细阅读它们。感谢您的意见!

标签: python pandas dataframe mean


【解决方案1】:

我认为您需要通过 iloc 过滤掉前 3 行,然后得到 mean

s = concatenated_df.iloc[3:].mean()

或者可以通过skiprows中的参数read_csv过滤掉前3行:

df_from_each_file = (pd.read_csv(f,delim_whitespace=0, usecols=[1], skiprows=[1,2,3]) 
                     for f in all_files)
concatenated_df  = pd.concat(df_from_each_file, axis=1)

s = concatenated_df.mean()

【讨论】:

  • 谢谢!!第二个选项有效,不幸的是第一个没有,仍然得到: Series([], dtype: float64) 尽管如此,非常感谢!
猜你喜欢
  • 2014-09-23
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2020-10-18
相关资源
最近更新 更多