首页 > 解决方案 > 如何使用 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

如果有人知道该怎么做,请告诉我!

标签: pythonpandasnumpydataframedate

解决方案


output_df = df[df['timestamp'] == date][['timestamp', 'users_holding']]

推荐阅读