简体   繁体   English

如何从 dataframe python 中提取特定的键和值

[英]how to extract specific key and value from a dataframe python

[在此处输入图片描述][1][

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.

相关问题 如何从列表中提取特定的键值对并将其添加到数据框? - How to extract specific key,value pairs from list of lists and add it to dataframe? 如何从python中的字典中提取特定值 - How to extract a specific value from a dictionary in python python - 通过变量从json文件中提取特定的键/值 - python - extract a specific key / value from json file by a variable 如何从python中的嵌套字典中提取特定键的值并返回值列表 - How to extract a specific key's value from a nested dictionary in python and return a list of the values 如何提取特定键的值,并使用 Python 将其添加到变量中? - How to extract the value of a specific key, and add it to a variable using Python? 当键值对被深度嵌套时,如何从 Python 中的这个 Json 可读提取物中返回特定的键值对? - How to return specific key value pairs from this Json readable extract in Python when key value pair is deeply nested? 如何提取特定编号来自 dataframe 列 Python 的字符 - How to extract specific no. of character from dataframe column Python 如何根据条件 python 从 dataframe 中提取特定内容 - how to extract specific content from dataframe based on condition python 如何从python中的数据帧行中提取特定长度的范围? - How to extract ranges with specific length from dataframe row in python? 从 json 中提取键和值到新的 dataframe - Extract key and value from json to new dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM