命令是“要求用户输入他想要接收数据的对象的名称和过滤器的名称, 您需要的数据(可以引入多个过滤器)。”
这是我的数据:
|Object | HJD 24... | Filter | Magnitude|
|-------|-------------|-----------|----------|
|SU_Hor | 55896.30476 | B |14.877 |
SU_Hor 55896.27438 Ic 13.885
SU_Hor 55896.27349 B 14.809
SU_Hor 55896.27397 V 14.434
SU_Hor 55896.40882 Ic 14.033
SU_Hor 55896.40829 V 14.540
SU_Hor 55896.40770 B 14.941
SU_Hor 55896.34973 Ic 13.958
SU_Hor 55896.34943 V 14.494
SU_Hor 55896.34906 B 14.861
SU_Hor 55896.30542 Ic 13.912
SU_Hor 55896.30512 v 14.440
SU_Hor 55897.38547 V 14.536
SU_Hor 55897.28281 B 14.882
SU_Hor 55897.28317 V 14.428
SU_Hor 55897.28347 Ic 13.927
RZ_Lyr 27359.3030 V 10.630
RZ_Lyr 27684.4510 V 10.610
RZ_Lyr 27685.4780 V 10.580
RZ_Lyr 27701.3150 V 10.700
RZ Lyr 27934.4560 V 10.660
RZ Lyr 27955.4100 V 10.570
rzlyr 30604.2000 V 11.030
RZ_Lyr 55314.5695 B 12.047
RZ_Lyr 55314.5724 B 12.036
RZ_Lyr 55314.5900 B 12.042
RZ_Lyr 55314.6105 B 12.045
RZ_Lyr 55314.6163 B 12.027
RZ_Lyr 55342.3509 B 12.057
RZLyr 55342.3557 B 12.058
RZ_Lyr 55342.3606 B 12.052
RZ_Lyr 55342.3654 B 12.058
这是我的代码:
def searchByfilter():
filter = input('Enter filter to show data \n')
df = pd.read_csv('Python_2ndLab.csv')
print(df.loc[df.filter == filter, :])
print('Enter 1 to search by object name')
print('Enter 2 to search by filter')
src = (input('Enter here: '))
if src == '1':
searchByObject()
elif src == '2':
searchByfilter()
else:
print('Sorry, invalid input')
我正在尝试解决,但仍然没有成功,并且出现错误:
Enter 2 to search by filter
Enter here: 2
Enter filter to show data
B
Traceback (most recent call last):
File "C:\Users\Acer\PycharmProjects\2nd Lab\main.py", line 78, in <module>
searchByfilter()
File "C:\Users\Acer\PycharmProjects\2nd Lab\main.py", line 64, in searchByfilter
print(df.loc[df.filter == filter, :])
File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1067, in __getitem__
return self._getitem_tuple(key)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1247, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 967, in _getitem_lowerdim
section = self._getitem_axis(key, axis=i)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1311, in _getitem_axis
self._validate_key(key, axis)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1118, in _validate_key
raise KeyError(
KeyError: 'False: boolean label can not be used without a boolean index'
当我输入“B”时,我希望收到来自所有“B”过滤器的数据
filter
是一种dataframe方法。将 df.filter
替换为 df['filter']
。
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.filter.html