日期 | 公司 | 收盘 | 开盘 | 高 | 低 | 交易量 | 涨跌幅 |
2023/7/12 | JD | 37.41 | 37.34 | 37.83 | 36.91 | 11.42M | 3.86% |
2023/7/11 | JD | 36.02 | 35.9 | 36.39 | 35.31 | 6.95M | 0.19% |
2023/7/10 | JD | 35.95 | 35.3 | 36.15 | 35.06 | 8.33M | 0.53% |
2023/7/12 | BABA | 94 | 94.11 | 95.03 | 92.55 | 23.78M | 2.41% |
2023/7/11 | BABA | 91.79 | 91.02 | 92.32 | 89.01 | 19.74M | 1.36% |
2023/7/10 | BABA | 90.56 | 90.05 | 92.04 | 89.6 | 25.19M | 0.01% |
2023/7/12 | BIDU | 148.83 | 147.44 | 150.42 | 145.5 | 2.41M | 3.84% |
2023/7/11 | BIDU | 143.33 | 143.23 | 144.45 | 140.01 | 960.45K | 0.27% |
2023/7/10 | BIDU | 142.95 | 140.72 | 143.95 | 140.12 | 887.82K | 0.20% |
2023/7/12 | IQ | 5.7 | 5.43 | 5.78 | 5.41 | 19.24M | 8.99% |
2023/7/11 | IQ | 5.23 | 5.15 | 5.3 | 5.12 | 5.23M | 2.15% |
2023/7/10 | IQ | 5.12 | 5.1 | 5.19 | 5.02 | 8.52M | -0.78% |
这个表格是代码中使用的数据,因为csdn上面没办法传表格。宝子们,如果想使用可以粘贴到自己的excel中保存使用
startswith()函数
描述:判断字符串是否以指定字符或子字符串开头。
语法:str.endswith("suffix", start, end) 或 str[start,end].endswith("suffix") 用于判断字符串中某段字符串是否以指定字符或子字符串结尾。 bool 返回值为布尔类型(True,False)
suffix 后缀,可以是单个字符,也可以是字符串,还可以是元组("suffix"中的引号要省略)。
str.contains(pat, case=True, flags=0, na=nan, regex=True)是否包含查找的字符串
pat : 字符串/正则表达式
case : 布尔值, 默认为True.如果为True则匹配敏感
flags : 整型,默认为0(没有flags)
na : 默认为NaN,替换缺失值.
regex : 布尔值, 默认为True.如果为真则使用re.research,否则使用Python
返回值:
布尔值的序列(series)或数组(array)
import pandas as pd
#查询数据
stu=pd.read_excel("D:/股票.xlsx")
condition=stu['公司']=='JD'
condition1=stu['日期']=='2023-07-11'
#多条件
condition2=stu['公司']=='IQ'
condition3="日期=='2023-07-11' and 开盘>=35.00"
#筛选数据
condition4="公司 in ['BABA','BIDU']"
condition5=stu.loc[2:4]
#筛选公司名字开头为B的公司
condition6=stu['公司'].str.startswith('B')
#筛选公司名中含有D的公司
condition7=stu['公司'].str.contains('D')
print(stu[condition])
print('-'*50)
print(stu[condition1])
print('-'*50)
print(stu.loc[condition2,['收盘','开盘']])
print('-'*50)
print(stu.query(condition3))
print('-'*50)
print(stu.query(condition4))
print('-'*50)
print(condition5)
print('-'*50)
print(stu[condition6])
print('-'*50)
print(stu[condition7])
运行结果(里面会出现未来警告,可以不用管):
D:\Python\p\Scripts\python.exe "D:\Python\python-learning\数据分析(二)\数据.py"
日期 公司 收盘 开盘 高 低 交易量 涨跌幅
0 2023-07-12 JD 37.41 37.34 37.83 36.91 11.42M 0.0386
1 2023-07-11 JD 36.02 35.90 36.39 35.31 6.95M 0.0019
2 2023-07-10 JD 35.95 35.30 36.15 35.06 8.33M 0.0053
--------------------------------------------------
日期 公司 收盘 开盘 高 低 交易量 涨跌幅
1 2023-07-11 JD 36.02 35.90 36.39 35.31 6.95M 0.0019
4 2023-07-11 BABA 91.79 91.02 92.32 89.01 19.74M 0.0136
7 2023-07-11 BIDU 143.33 143.23 144.45 140.01 960.45K 0.0027
10 2023-07-11 IQ 5.23 5.15 5.30 5.12 5.23M 0.0215
--------------------------------------------------
收盘 开盘
9 5.70 5.43
10 5.23 5.15
11 5.12 5.10
--------------------------------------------------
D:\Python\python-learning\数据分析(二)\数据.py:36: FutureWarning: The behavior of 'isin' with dtype=datetime64[ns] and castable values (e.g. strings) is deprecated. In a future version, these will not be considered matching by isin. Explicitly cast to the appropriate dtype before calling isin instead.
print(stu.query(condition3))
日期 公司 收盘 开盘 高 低 交易量 涨跌幅
1 2023-07-11 JD 36.02 35.90 36.39 35.31 6.95M 0.0019
4 2023-07-11 BABA 91.79 91.02 92.32 89.01 19.74M 0.0136
7 2023-07-11 BIDU 143.33 143.23 144.45 140.01 960.45K 0.0027
--------------------------------------------------
日期 公司 收盘 开盘 高 低 交易量 涨跌幅
3 2023-07-12 BABA 94.00 94.11 95.03 92.55 23.78M 0.0241
4 2023-07-11 BABA 91.79 91.02 92.32 89.01 19.74M 0.0136
5 2023-07-10 BABA 90.56 90.05 92.04 89.60 25.19M 0.0001
6 2023-07-12 BIDU 148.83 147.44 150.42 145.50 2.41M 0.0384
7 2023-07-11 BIDU 143.33 143.23 144.45 140.01 960.45K 0.0027
8 2023-07-10 BIDU 142.95 140.72 143.95 140.12 887.82K 0.0020
--------------------------------------------------
日期 公司 收盘 开盘 高 低 交易量 涨跌幅
2 2023-07-10 JD 35.95 35.30 36.15 35.06 8.33M 0.0053
3 2023-07-12 BABA 94.00 94.11 95.03 92.55 23.78M 0.0241
4 2023-07-11 BABA 91.79 91.02 92.32 89.01 19.74M 0.0136
--------------------------------------------------
日期 公司 收盘 开盘 高 低 交易量 涨跌幅
3 2023-07-12 BABA 94.00 94.11 95.03 92.55 23.78M 0.0241
4 2023-07-11 BABA 91.79 91.02 92.32 89.01 19.74M 0.0136
5 2023-07-10 BABA 90.56 90.05 92.04 89.60 25.19M 0.0001
6 2023-07-12 BIDU 148.83 147.44 150.42 145.50 2.41M 0.0384
7 2023-07-11 BIDU 143.33 143.23 144.45 140.01 960.45K 0.0027
8 2023-07-10 BIDU 142.95 140.72 143.95 140.12 887.82K 0.0020
--------------------------------------------------
日期 公司 收盘 开盘 高 低 交易量 涨跌幅
0 2023-07-12 JD 37.41 37.34 37.83 36.91 11.42M 0.0386
1 2023-07-11 JD 36.02 35.90 36.39 35.31 6.95M 0.0019
2 2023-07-10 JD 35.95 35.30 36.15 35.06 8.33M 0.0053
6 2023-07-12 BIDU 148.83 147.44 150.42 145.50 2.41M 0.0384
7 2023-07-11 BIDU 143.33 143.23 144.45 140.01 960.45K 0.0027
8 2023-07-10 BIDU 142.95 140.72 143.95 140.12 887.82K 0.0020
进程已结束,退出代码为 0