系统:Windows 10 编辑器:JetBrains PyCharm Community Edition 2018.2.2 x64 pandas:1.1.5
- 这个系列讲讲Python的科学计算及可视化
- 今天讲讲pandas模块
- 将df按某列进行排序
Part 1:场景描述
- 已知df1,包括6列,
"time", "pos", "value1", "value2", "value3", "value4
- 其中value4为周次信息,想获取最新周次value1的取值
- 如下图,最新的周次应该为21KW36,其对应value1的取值为50
df
Part 2:逻辑
- 将df按照value4列进行排序
- 取第1行value1的取值即为所求
Part 3:代码
import pandas as pd
dict_1 = {"time": ["2019-11-02", "2019-11-03", "2019-11-04", "2019-11-05",
"2019-12-02", "2019-12-03", "2019-12-04", "2019-12-05"],
"pos": ["A", "A", "C", "D", "E", "E", "G", "H"],
"value1": [10, 20, 30, 40, 50, 50, 70, 80],
"value2": [100, 200, 300, 400, 500, 600, 700, 800],
"value3": [50, 20, 30, 90, 50, 60, 80, 80],
"value4": ['21W12', '21W10', '21W01', '21W05', '21W06', '21W36', '21W21', '21W23']}df_1 = pd.DataFrame(dict_1, columns=["time", "pos", "value1", "value2", "value3", "value4"])
print("\n", "df_1", "\n", df_1, "\n")df_1.sort_values(by='value4', ascending=False, inplace=True)
print("\n", "排序后df_1", "\n", df_1, "\n")
第1行第3列
val = df_1.iloc[0, 2]
remarks = "最新周value1取值:{0}".format(val)print(remarks)
代码截图
执行结果
Part 4:部分代码解读
df_1.sort_values(by='value4', ascending=False, inplace=True)
,将df_1按照value4列进行排序,且排序方法为降序,ascending=False表示为降序,ascending为上升的意思df_1.sort_values(by='value4', ascending=True, inplace=True)
即按照升序来排序,结果如下图
val = df_1.iloc[0, 2]
,获取第1行第3列的取值,即value1列的取值。iloc为按照位置来取值,注意与loc的区别
升序
本文为原创作品,欢迎分享朋友圈