Pandas实战——DataFrame

2021-09-10  本文已影响0人  深思海数_willschang
pandas in action.png

原书地址

本篇笔记为原书第四章节的内容。


DataFrame是二维行列的表格型数据结构。如下图所示5行2列:


DataFrame

DataFrame创建

city_data = {
        "City": ["New York City", "Paris", "Barcelona", "Rome"],
        "Country": ["United States", "France", "Spain", "Italy"],
        "Population": [8600000, 2141000, 5515000, 2873000]
    }
cities = pd.DataFrame(city_data)
cities
from dict
pd.DataFrame(np.random.randint(1, 101, [3, 5]))
from narray
random_data = np.random.randint(1, 101, [3, 5])
row_labels = ["Morning", "Afternoon", "Evening"]
column_labels = (
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
    )

pd.DataFrame(
    data = random_data,
    index = row_labels,
    columns = column_labels,
)
index && columns

DataFrame属性及方法

属性

方法

DataFrame排序操作

sort_values()
sort_index()
sort_index
对列标签进行排序
axis params
设置新index

取DataFrame的行或列值

取列值

取行值

对行列重命名

上一篇下一篇

猜你喜欢

热点阅读