简体   繁体   English

pandas DataFrame 行的类字典 get() 方法?

[英]dictionary-like get() method for rows of a pandas DataFrame?

Is there a better, more idiomatic way to do this?有没有更好、更惯用的方法来做到这一点?

import pandas as pd
df : pd.DataFrame

try:
   row = df.loc[key]
except KeyError:
   row = None

If I wanted a column instead of a row I could just use df.get(key) .如果我想要一列而不是一行,我可以使用df.get(key) I tried df.transpose().get(key) , which does work, but the transpose is not just a view, it physically transposes the data so it is quite slow when used for this purpose.我尝试df.transpose().get(key) ,它确实有效,但转置不仅仅是一个视图,它在物理上转置数据,因此在用于此目的时它非常慢。

Use reindex , but that would return a series of NaN :使用reindex ,但这会返回一系列NaN

df.reindex([key]).loc[key]

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

相关问题 将嵌套的类似字典的 txt 文件读入 Pandas 数据帧 - Reading nested dictionary-like txt file into a Pandas dataframe 将包含重复变量名称和变量值的类字典对象列表转换为 Pandas dataframe - Converting a list of dictionary-like objects containing repeated variable names and variable values to a Pandas dataframe 如何获取类似字典的字符串中的值,并在 dataframe 的列中对 output 进行排序? - How to get values in a dictionary-like string and sort the output all within the column in a dataframe? Python:数据框中的类字典对象到新数据框 - Python: Dictionary-like object in dataframe to new dataframe 如果值采用类似字典的格式,则从熊猫系列中提取数据 - Extract data from a pandas series if the values are in a dictionary-like format 如何在类似字典的字符串的列中获取值? - How to get values in a column of dictionary-like string? 将类似字典的输入转换成字典 - Transform Dictionary-Like Input to a Dictionary Dataframe 到字典,按 pandas 中的行 - Dataframe to dictionary, by rows in pandas 如何从熊猫中杂乱无章的字典状数据框中提取分数? - How to extract a score from a disorganised dictionary-like data frame in pandas? 高效的数据结构可让熊猫般的字典式观察按时间在关键时刻锁定? - Efficient data structure to keep dictionary-like observations keyed by time in Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM