Python 如何使用pandas在datafram中搜索匹配字符串?

Python 如何使用pandas在datafram中搜索匹配字符串?,python,pandas,numpy,dataframe,date,Python,Pandas,Numpy,Dataframe,Date,我是一名初级程序员,使用Python中的pandas库。我目前有一个代码,根据我提供的程序标题,读取从我桌面上提取的csv文件: string = input("Enter a ticker: ") file = string + (".csv") file_location = 'M:\\A*************\\r*******\\files\\r************.tar\\tmp\\p*********\\' + file if co

我是一名初级程序员,使用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列(时间戳,用户持有),大约17k行

我希望我的用户能够输入日期:

date = input("Enter a date (YYYY-MM-DD): ")
程序将返回带有相应日期(时间戳)和用户(用户)的行。 前/


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

用户给定的日期可以作为变量dt接收

output_df = df[df['timestamp'] == date][['timestamp', 'users_holding']]
#Import libraries and and convert user given date to datetime:
from datetime import datetime
dt1 = datetime.strptime("2019-08-23", "%Y-%m-%d")\

#Do similar conversion for your Date column in pandas DF
#Filter all the dates equal to your date
df[ df['date'] == dt1 ] ['timestamp', ' 'users_holding']
旁注。请不要使用诸如“timestamp”之类的列名,这可能会导致代码冲突。
请养成习惯,将这些列重命名为标准列,例如“用户时间戳”

您的用户给定日期可以作为变量dt接收

#Import libraries and and convert user given date to datetime:
from datetime import datetime
dt1 = datetime.strptime("2019-08-23", "%Y-%m-%d")\

#Do similar conversion for your Date column in pandas DF
#Filter all the dates equal to your date
df[ df['date'] == dt1 ] ['timestamp', ' 'users_holding']
旁注。请不要使用诸如“timestamp”之类的列名,这可能会导致代码冲突。
请养成习惯,将这些列重命名为“用户时间戳”之类的标准列。

您不需要搜索字符串,应该使用正确的熊猫。时间戳您不需要搜索字符串,应该使用正确的熊猫。时间戳不使用链式索引。一次完成所有操作:
df.loc[df['timestamp']==date,['timestamp','users\u holding']]
```空数据帧列:[timestamp,users\u holding]索引:[]不要使用链式索引。一气呵成:
df.loc[df['timestamp']==date,['timestamp','users\u holding']
```空数据帧列:[timestamp,users\u holding]索引:[]