Pandas基础之查找和筛选数据

日期公司收盘开盘交易量涨跌幅
2023/7/12JD37.4137.3437.8336.9111.42M3.86%
2023/7/11JD36.0235.936.3935.316.95M0.19%
2023/7/10JD35.9535.336.1535.068.33M0.53%
2023/7/12BABA9494.1195.0392.5523.78M2.41%
2023/7/11BABA91.7991.0292.3289.0119.74M1.36%
2023/7/10BABA90.5690.0592.0489.625.19M0.01%
2023/7/12BIDU148.83147.44150.42145.52.41M3.84%
2023/7/11BIDU143.33143.23144.45140.01960.45K0.27%
2023/7/10BIDU142.95140.72143.95140.12887.82K0.20%
2023/7/12IQ5.75.435.785.4119.24M8.99%
2023/7/11IQ5.235.155.35.125.23M2.15%
2023/7/10IQ5.125.15.195.028.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

  • 36
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值