python - How to get the user Input in Pandas for a particular column from the dataframe python3
问题描述
I'm using PANDAS Dataframe to get the data from CSV file and opting to choose the desired column data which is working fine. However, in the hostData
Dataframe i'm looking specially for (data['Safe']=='KDS-PDC-DEFAULT-UNIX-ROOT')
portion so, as i have multiple safes as i mentioned below the CyberArk Safes
within the doc string hence i'm looking forward to rather putting the safe name manually on the code it could ask on the user input.
While asking for the User Input to give the safe name if it could display the current safe names which is within doc string.
#!/grid/common/pkgs/python/v3.6.1/bin/python3
from __future__ import print_function
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
import csv
import pandas as pd
##### Python pandas, widen output display to see more columns. ####
pd.set_option('display.height', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('expand_frame_repr', True)
############# CyberArk Safes ################
'''
KDS-KDC-DEFAULT-UNIX-ROOT
KDS-PDC-DEFAULT-UNIX-ROOT
KDS-CDC-DEFAULT-UNIX-ROOT
'''
################# PANDAS Extraction ###########
data = pd.read_csv('/home/karn/plura/Test/Python_Panda/InventoryReports06.csv', usecols=['Platform ID', 'Safe', 'Target system address', 'Failure reason'])
hostData = data[(data['Safe']=='DC') | (data['Safe']=='KDS-PDC-DEFAULT-UNIX-ROOT')][['Safe', 'Target system address']]
hostData.reset_index(level=0, drop=True)
Expected Output:
$ python CyberSafe.py
The Current SafeName Available are:
KDS-KDC-DEFAULT-UNIX-ROOT
KDS-PDC-DEFAULT-UNIX-ROOT
KDS-CDC-DEFAULT-UNIX-ROOT
Please Enter the SafeName:
Any help or suggestion will highly be appreciated.
解决方案
- For displaying candidates, create a function (e.g.
print_candidates()
) to show a docstring. - For getting user inputs, use
input()
and store it toSafeName
and further use it to create a mask to filter the dataframe.
Therefore the later part of your code would be something like below:
def print_candidates():
'''
KDS-KDC-DEFAULT-UNIX-ROOT
KDS-PDC-DEFAULT-UNIX-ROOT
KDS-CDC-DEFAULT-UNIX-ROOT
'''
print("The Current SafeName Available are:")
print(print_candidates.__doc__)
print_candidates()
################# PANDAS Extraction ###########
data = pd.read_csv('/home/karn/plura/Test/Python_Panda/InventoryReports06.csv', usecols=['Platform ID', 'Safe', 'Target system address', 'Failure reason'])
SafeName = str(input("Please Enter the SafeName:"))
hostData = data.loc[(data['Safe']=='DC') | (data['Safe']==SafeName)][['Safe', 'Target system address']]
hostData = hostData.reset_index(drop=True)
推荐阅读
- angular - 如何修改 Angular Material 日期范围选择器以仅接受月份
- google-sheets - 如何将 Google 表格中的 Twitter 日期/时间值解析为日期时间评估的格式
- sql - 为什么这不是对所有 form_id 进行排序,我怎样才能对其进行排序
- time-complexity - 自动编码器的顺序复杂度
- python - 禁止和取消禁止仅在没有角色的情况下工作
- ios - flutter in_app_purchase:如何在 iOS 上验证收据?
- pine-script - 如何根据条件为真触发标签,但仅触发一次
- java - 如何知道 java.util.concurrent 未来接口是否仍然有效?
- python - 通过函数添加减法值并将数据附加到 for 循环的列表中
- python - \b 等效于在 python 中保持零宽度的 unicode 正则表达式