pandas - 基础属性

复制代码
import pandas as pd


# pd.set_option('display.unicode.east_asian_width', True)  # 规整格式
# df = pd.read_excel(r'C:\Users\hui\Desktop\统计结果(1).xlsx')
# new_df = df.T
# print(df.dtypes)  # 查看属性
# print(df.columns)  # 查看列索引
# print(new_df)  # 行列数据转换
# print(df.head(2))  # 查看前N条数据
# print(df.tail(2))  # 查看后N条数据
# print(df.shape[0], df.shape[1])  # 查看多少行,多少列。shape[0]表示多少行,shape[1]表示多少列
# print(df.info)  # 查看索引,数据类型,内存信息

# DataFrame 重要功能函数
# print(df.describe())
# print(df.count())
# print(df['恢复总数'].sum())  # 求和
# print(df.max())  # 最大值
# print(df.min())  # 最小值
复制代码
复制代码
在 Pandas 中,DataFrame 和 Series 是两个核心的数据结构。下面是一些 Pandas 基础属性的介绍:

1.DataFrame:

  shape:返回 DataFrame 的行数和列数。
  columns:返回 DataFrame 的列标签。
  index:返回 DataFrame 的行索引。
  dtypes:返回 DataFrame 中每列的数据类型。
  head(n):返回 DataFrame 的前 n 行数据,默认为前 5 行。
  tail(n):返回 DataFrame 的后 n 行数据,默认为后 5 行。
2.Series:

  values:返回 Series 的值部分,以 Numpy 数组形式展示。
  index:返回 Series 的索引。
  dtype:返回 Series 的数据类型。
  head(n):返回 Series 的前 n 个值,默认为前 5 个值。
  tail(n):返回 Series 的后 n 个值,默认为后 5 个值。
复制代码
复制代码
import pandas as pd

# 示例 DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'City': ['New York', 'London', 'Paris', 'Tokyo']}
df = pd.DataFrame(data)

# 示例 Series
s = pd.Series([10, 20, 30, 40, 50], index=['a', 'b', 'c', 'd', 'e'])
复制代码
复制代码
# DataFrame 属性
print("DataFrame shape:", df.shape)
print("DataFrame columns:", df.columns)
print("DataFrame index:", df.index)
print("DataFrame dtypes:", df.dtypes)
print("DataFrame head:")
print(df.head())
print("DataFrame tail:")
print(df.tail())

# Series 属性
print("Series values:", s.values)
print("Series index:", s.index)
print("Series dtype:", s.dtype)
print("Series head:")
print(s.head())
print("Series tail:")
print(s.tail())
复制代码

 

posted @   徐俊112  阅读(34)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示