【问题标题】:How do I print the dimensions of a dataset (csv file) using Pandas- library and also print out some lines?如何使用 Pandas 库打印数据集(csv 文件)的尺寸并打印出一些行?
【发布时间】:2018-10-09 16:07:28
【问题描述】:

所以我正在使用 Python 3 进行编程,并且想使用 pandas 库数据框打印出数据集(csv 文件)的维度,并且还做一些我不太了解的其他事情?这只是一个例子,因为我只需要解释如何。 假设我有 2 个功能:

在 func1 中,我(据说)使用 pandas 加载了一个数据集:

def func1(a):

namesOfColumns = ["The sepal-length", "The sepal-width", "The petal-length", "The petal-width", "class"]

a = "some_file"

some_file = pd.read_csv(a)

return (some_file)

def func2(数据):

#code for printing the dimensions of the dataset
#code for printing the top 3 lines
#code for printing the mean and standard variation of the sepal-width
#code for plot box plot of each attribute

有人能解释我如何处理 func2 中的步骤吗?

【问题讨论】:

  • 打印尺寸:a.shape,打印前 n 行:a.head(n),打印标准,平均值:df[column].std(),df[column].mean()

标签: python python-3.x pandas csv dataframe


【解决方案1】:

打印数据集维度的代码:

print(data.info())  # Descriptive info about the DataFrame
print(data.shape)  # gives a tuple with the shape of DataFrame

打印前 3 行的代码:

print(data.head(3))

打印萼片宽度的平均值和标准变化:

print(data.describe())  # General statistics
print(data['Sepal_Width'].mean(), data['Sepal_Width'].std())  # Mean & std dev of Sepal_Width only

每个属性的箱线图代码:

data.boxplot(namesOfColumns)

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多