python - 如何使用 pandas 在数据框中搜索匹配的字符串?
问题描述
我是使用 Python 中的 pandas 库的初学者编码器。我目前有一个代码可以读取从我的桌面提取的 csv 文件,具体取决于我提供的程序标题:
string = input("Enter a ticker: ")
file = string + (".csv")
file_location = 'M:\\A*************\\r*******\\files\\r************.tar\\tmp\\p*********\\' + file
if config.is_file():
print(file)
print(file_location)
df = pd.read_csv(file_location)
#print(df)
else:
print("Invalid ticker input")
这允许我的用户输入文件名,并使用 Pandas 读取文件。数据框为 2 列(时间戳、users_holding)和大约 17k 行。
我希望我的用户能够输入日期:
date = input("Enter a date (YYYY-MM-DD): ")
并且程序将返回具有相应日期(时间戳)和用户(users_holding)的行。前任/
enter ticker: TSLA
enter a date (YYYY-MM-DD): 2019-08-23
timestamp users_holding
2019-08-23 15,097
如果有人知道该怎么做,请告诉我!
解决方案
output_df = df[df['timestamp'] == date][['timestamp', 'users_holding']]
推荐阅读
- policy - 无效操作的策略梯度 (REINFORCE)
- python - E0401: 无法导入“烧瓶”
- c# - 为什么 ReSharper 告诉我局部变量未使用并且我正在访问修改后的闭包?
- laravel-5.6 - 我想在 foreach 循环 laravel 5.6 中计算 if 语句
- python - 如何解决这个错误开始?
- javascript - 带有 babel-preset-env 的 gulp-babel 忽略 browserslist
- c - C中的“if-else”歧义是什么?
- mysql - 使用 GroupBy 时在 MySQL 中随机选择子查询
- ruby-on-rails - Rails 5.2 ActionController::InvalidAuthenticityToken
- javascript - 使用 vue.js 格式化 json 文件