简体   繁体   English

如何通过 input() 在 pandas Dataframe 中按内容查找索引?

[英]How to find an index by content via input() in pandas Dataframe?

I am trying to find an index based on the content of a table, which in this case is a string in the form of a date 'd/m/YYYY' or 'dd/mm/YYYY' for 2-digit days and months respectively.我正在尝试根据表的内容查找索引,在本例中,它是日期形式为 'd/m/YYYY' 或 'dd/mm/YYYY' 的字符串,表示 2 位数的日期和月份分别。 I am looking for the 1st of a specific month which i want to find interactively using input().我正在寻找我想使用 input() 以交互方式查找的特定月份的第一天。

The code works fine as long as i hardcode the month:只要我对月份进行硬编码,代码就可以正常工作:

stream = open(sys.argv[1], 'r')
for path in stream:
    df = pd.read_csv('{}'.format(path))
    df['Idx'] = df['Date'].str.contains('^1/8/20\d{2} 0:00:00')
    index_month = df[df['Idx']==True].index.values.astype(int)[0]
    print('Index was found at {}'.format(month, index_month))

Output: Output:

Index was fount at 19516
Index was found at 19527
...
...

but gives但给

ValueError: cannot switch from automatic field numbering to manual field specification

when i use input() to find the respective month (see code below)当我使用input()查找相应的月份时(请参见下面的代码)

print("Enter Month as number:")
month=input()

stream = open(sys.argv[1], 'r')
for path in stream:
    df = pd.read_csv('{}'.format(path))
    df['Idx'] = df['Date'].str.contains('^1/{}/20\d{2} 0:00:00'.format(month))
    index_month = df[df['Idx']==True].index.values.astype(int)[0]
    print('Index was found at {}'.format(month, index_month))

The error is traced back to错误追溯到

df['Idx'] = df['Date'].str.contains('^1/{}/20\d{2} 0:00:00'.format(month))

Is it possible to solve it this way, or is there a better approach?是否可以通过这种方式解决,或者有更好的方法吗?

Your problem is simply here:你的问题就在这里:

'^1/{}/20\d{2} 0:00:00'

{2} is interpreted by str's format method, so you need to escape it using double braces: {2}由 str 的format方法解释,因此您需要使用双大括号将其转义:

>>> month = 3
>>> re.search('^1/{}/20\d{{2}} 0:00:00'.format(month), '1/3/2020 0:00:00').group()
'1/3/2020 0:00:00'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到具有类似关键字的熊猫数据框索引? - how to find a pandas dataframe index with a similar keyword? 如何在 pandas DataFrame 中找到第二个最小值的索引? - How to find the index of the second minimum in pandas DataFrame? 如何找到给定 Pandas 数据帧索引的位置索引? - How to find the location index of a given Pandas dataframe index? 如何在 dataframe 列 pandas 中找到最接近所选数字的 5 个数字的索引? - How to find the index of 5 numbers nearest to a chosen number in a dataframe column pandas? 如何找到pandas dataframe中满足条件的数据的索引和列? - How to find the index and columns of data in pandas dataframe that meet the condition? 如何在 pandas dataframe 的列中以字符串格式查找特定值的索引? - How to find index in a string format for a particular value in a column of a pandas dataframe? 如何在 ndarray 中找到索引的值并转换为 pandas Dataframe? - How do I find the value of an index in an ndarray and convert to pandas Dataframe? 如何在 Pandas DataFrame 中找到第一个唯一元素的索引? - How to find index of the first unique elements in Pandas DataFrame? 如何在 Pandas 数据框列中找到已知值的索引? - How do I find the index of a known value in a pandas dataframe column? 如何在大 Pandas DataFrame 中找到“True”值对应的索引和列? - How to find the `True` values' corresponding index and column in a large Pandas DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM