06 DataFrame的赋值和排序
修改某列的值
示例代码:
import pandas as pd
# 读取csv文件数据
df = pd.read_csv("./data/stock_day.csv")
# 删除指定列
df = df.drop(["ma5", "ma10", "ma20", "v_ma5", "v_ma10", "v_ma20"], axis=1)
print(df.head())
print("==============")
# 修改某列的值
df["close"] = 1
print(df.head())
print("==============")
输出结果:
open high close low volume price_change p_change turnover
2018-02-27 23.53 25.88 24.16 23.53 95578.03 0.63 2.68 2.39
2018-02-26 22.80 23.78 23.53 22.80 60985.11 0.69 3.02 1.53
2018-02-23 22.88 23.37 22.82 22.71 52914.01 0.54 2.42 1.32
2018-02-22 22.25 22.76 22.28 22.02 36105.01 0.36 1.64 0.90
2018-02-14 21.49 21.99 21.92 21.48 23331.04 0.44 2.05 0.58
==============
open high close low volume price_change p_change turnover
2018-02-27 23.53 25.88 1 23.53 95578.03 0.63 2.68 2.39
2018-02-26 22.80 23.78 1 22.80 60985.11 0.69 3.02 1.53
2018-02-23 22.88 23.37 1 22.71 52914.01 0.54 2.42 1.32
2018-02-22 22.25 22.76 1 22.02 36105.01 0.36 1.64 0.90
2018-02-14 21.49 21.99 1 21.48 23331.04 0.44 2.05 0.58
==============
根据某一列降序
示例代码:
import pandas as pd
# 读取csv文件数据
df = pd.read_csv("./data/stock_day.csv")
# 删除指定列
df = df.drop(["ma5", "ma10", "ma20", "v_ma5", "v_ma10", "v_ma20"], axis=1)
print(df.head())
print("==============")
# 根据开盘价降序
df1 = df.sort_values(by="open", ascending=False).head()
print(df1.head())
print("==============")
输出结果:
open high close low volume price_change p_change turnover
2018-02-27 23.53 25.88 24.16 23.53 95578.03 0.63 2.68 2.39
2018-02-26 22.80 23.78 23.53 22.80 60985.11 0.69 3.02 1.53
2018-02-23 22.88 23.37 22.82 22.71 52914.01 0.54 2.42 1.32
2018-02-22 22.25 22.76 22.28 22.02 36105.01 0.36 1.64 0.90
2018-02-14 21.49 21.99 21.92 21.48 23331.04 0.44 2.05 0.58
==============
open high close low volume price_change p_change turnover
2015-06-15 34.99 34.99 31.69 31.69 199369.53 -3.52 -10.00 6.82
2015-06-12 34.69 35.98 35.21 34.01 159825.88 0.82 2.38 5.47
2015-06-10 34.10 36.35 33.85 32.23 269033.12 0.51 1.53 9.21
2017-11-01 33.85 34.34 33.83 33.10 232325.30 -0.61 -1.77 5.81
2015-06-11 33.17 34.98 34.39 32.51 173075.73 0.54 1.59 5.92
==============
根据某几列升序
示例代码:
import pandas as pd
# 读取csv文件数据
df = pd.read_csv("./data/stock_day.csv")
# 删除指定列
df = df.drop(["ma5", "ma10", "ma20", "v_ma5", "v_ma10", "v_ma20"], axis=1)
print(df.head())
print("==============")
# 根据开盘价和最高价升序
df2 = df.sort_values(by=["open", "high"]).head()
print(df2.head())
print("==============")
输出结果:
open high close low volume price_change p_change turnover
2018-02-27 23.53 25.88 24.16 23.53 95578.03 0.63 2.68 2.39
2018-02-26 22.80 23.78 23.53 22.80 60985.11 0.69 3.02 1.53
2018-02-23 22.88 23.37 22.82 22.71 52914.01 0.54 2.42 1.32
2018-02-22 22.25 22.76 22.28 22.02 36105.01 0.36 1.64 0.90
2018-02-14 21.49 21.99 21.92 21.48 23331.04 0.44 2.05 0.58
==============
open high close low volume price_change p_change turnover
2015-03-02 12.25 12.67 12.52 12.20 96291.73 0.32 2.62 3.30
2015-09-02 12.30 14.11 12.36 12.30 70201.74 -1.10 -8.17 2.40
2015-03-03 12.52 13.06 12.70 12.52 139071.61 0.18 1.44 4.76
2015-03-04 12.80 12.92 12.90 12.61 67075.44 0.20 1.57 2.30
2015-03-05 12.88 13.45 13.16 12.87 93180.39 0.26 2.02 3.19
==============
根据索引升序
示例代码:
import pandas as pd
# 读取csv文件数据
df = pd.read_csv("./data/stock_day.csv")
# 删除指定列
df = df.drop(["ma5", "ma10", "ma20", "v_ma5", "v_ma10", "v_ma20"], axis=1)
print(df.head())
print("==============")
# 根据索引升序
df2 = df.sort_index().head()
print(df2.head())
print("==============")
输出结果:
open high close low volume price_change p_change turnover
2018-02-27 23.53 25.88 24.16 23.53 95578.03 0.63 2.68 2.39
2018-02-26 22.80 23.78 23.53 22.80 60985.11 0.69 3.02 1.53
2018-02-23 22.88 23.37 22.82 22.71 52914.01 0.54 2.42 1.32
2018-02-22 22.25 22.76 22.28 22.02 36105.01 0.36 1.64 0.90
2018-02-14 21.49 21.99 21.92 21.48 23331.04 0.44 2.05 0.58
==============
open high close low volume price_change p_change turnover
2015-03-02 12.25 12.67 12.52 12.20 96291.73 0.32 2.62 3.30
2015-03-03 12.52 13.06 12.70 12.52 139071.61 0.18 1.44 4.76
2015-03-04 12.80 12.92 12.90 12.61 67075.44 0.20 1.57 2.30
2015-03-05 12.88 13.45 13.16 12.87 93180.39 0.26 2.02 3.19
2015-03-06 13.17 14.48 14.28 13.13 179831.72 1.12 8.51 6.16
==============
Series根据值升序
示例代码:
import pandas as pd
# 读取csv文件数据
df = pd.read_csv("./data/stock_day.csv")
# 删除指定列
df = df.drop(["ma5", "ma10", "ma20", "v_ma5", "v_ma10", "v_ma20"], axis=1)
print(df.head())
print("==============")
# Series根据值升序
s = df["high"].sort_values().head()
print(s)
输出结果:
open high close low volume price_change p_change turnover
2018-02-27 23.53 25.88 24.16 23.53 95578.03 0.63 2.68 2.39
2018-02-26 22.80 23.78 23.53 22.80 60985.11 0.69 3.02 1.53
2018-02-23 22.88 23.37 22.82 22.71 52914.01 0.54 2.42 1.32
2018-02-22 22.25 22.76 22.28 22.02 36105.01 0.36 1.64 0.90
2018-02-14 21.49 21.99 21.92 21.48 23331.04 0.44 2.05 0.58
==============
2015-03-02 12.67
2015-03-04 12.92
2015-03-03 13.06
2015-09-07 13.38
2015-03-05 13.45
Name: high, dtype: float64
Series根据索引升序
示例代码:
import pandas as pd
# 读取csv文件数据
df = pd.read_csv("./data/stock_day.csv")
# 删除指定列
df = df.drop(["ma5", "ma10", "ma20", "v_ma5", "v_ma10", "v_ma20"], axis=1)
print(df.head())
print("==============")
# Series根据索引升序
s = df["high"].sort_index().head()
print(s)
输出结果:
open high close low volume price_change p_change turnover
2018-02-27 23.53 25.88 24.16 23.53 95578.03 0.63 2.68 2.39
2018-02-26 22.80 23.78 23.53 22.80 60985.11 0.69 3.02 1.53
2018-02-23 22.88 23.37 22.82 22.71 52914.01 0.54 2.42 1.32
2018-02-22 22.25 22.76 22.28 22.02 36105.01 0.36 1.64 0.90
2018-02-14 21.49 21.99 21.92 21.48 23331.04 0.44 2.05 0.58
==============
2015-03-02 12.67
2015-03-03 13.06
2015-03-04 12.92
2015-03-05 13.45
2015-03-06 14.48
Name: high, dtype: float64
原文始发于微信公众号(张大鹏520):pandas如何根据指定某列或者某几列或者索引进行排序呢?
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/48677.html