[英]how to extract specific key and value from a dataframe python
I want to extract name(a) and respective scores(90) from a data frame As you can see here I used iloc method to get the scores of respective names.我想从数据框中提取名称(a)和相应的分数(90)正如您在此处看到的,我使用 iloc 方法来获取各个名称的分数。 The only problem is if names get interchanged in a dataframe we can't get accurate scores.
唯一的问题是,如果名称在 dataframe 中互换,我们将无法获得准确的分数。 Can we extract name as key and score as a value using python?
我们可以使用 python 提取名称作为键和分数作为值吗? By this way we can avoid hardcoding.
通过这种方式,我们可以避免硬编码。
Code:代码:
name_dict = {
'Name': ['a','b','c','d'],
'Score': [90,80,95,20]}
df = pd.DataFrame(name_dict)
print (df)
df.iloc[0,1]
Output: Output:
90
Set the index of the dataframe to the Name
column将 dataframe 的索引设置为
Name
列
df.set_index('Name', inplace=True)
Then you can fetch the score as然后你可以获取分数
df.loc['a', 'Score']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.