简体   繁体   English

如何在数据帧中为python中的特定行值选择列数据?

[英]How do I go about selecting column data in a dataframe for specific row values in python?

As the question says, I have a data frame which is quite large but looks like: 正如问题所说,我有一个非常大的数据框,但看起来像:

        ID    Count    ValueX    Value 2    Value 3
RowX    1      234.     255.       yes.      yes
RowY    1      123.     135.       543.      342
RowW    1      234.     235.       yes.      yes
RowJ    1      123.     115.       543.      342
RowA    1      234.     285.       yes.      yes
RowR    1      123.     165.       543.      342
RowX    2      234.     255.       yes.      yes
RowY    2      123.     135.       543.      342
RowW    2      234.     235.       yes.      yes
RowJ    2      123.     115.       543.      342
RowA    2      234.     285.       yes.      yes
RowR    2      123.     165.       543.      342
.
.
.
RowX    1233   234.     255.       yes.      yes
RowY    1233   123.     135.       543.      342
RowW    1233   234.     235.       yes.      yes
RowJ    1233   123.     115.       543.      342
RowA    1233   234.     285.       yes.      yes
RowR    1233   123.     165.       543.      342

What I want is to be able to select all the values in column ValueX where the row is RowX for each of the ID numbers 1-1233 and return them in a list. 我要的是能够选择在列中的所有值ValueX其中行是RowX每个ID号1-1233的并以列表返回它们。

df.query('1 <= ID <= 1233').loc['RowX', 'ValueX']

RowX    255.0
RowX    255.0
RowX    255.0
Name: ValueX, dtype: float64

IIUC: IIUC:

In [30]: df.loc[df.index.isin(['RowX']) & df['ID'].between(1, 1233), 'ValueX'].tolist()
Out[30]: [255.0, 255.0, 255.0]
filtered_df = df[(df.ID.between(1, 1233)) & (df.index == 'RowX')][['ValueX']]

values_list = filtered_df.ValueX.tolist()

暂无
暂无

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

相关问题 如何通过在 Python DataFrame 中保持某些列值不变来将数据从一行合并到另一行 - How do I merge data from one row to another by keeping some column values unchanged in Python DataFrame Python通告:如何处理这种特定情况? - Python circular reference : How do I go about this specific case? 如何根据 python 中特定列的不同值拆分/子集 dataframe - How do I split/subset a dataframe based on the distinct values of a specific column in python 根据 python dataframe 中的特定条件减去特定列的行值 - Subtracting values of a row for a specific column based on a specific condition in python dataframe 如何在Python中按特定行将列中的值划分? - How I can divide values in a column by specific row in Python? 从 pandas dataframe 的列中选择特定值 - Selecting specific values out of a column in pandas dataframe 我将如何创建一个具有 aa 列的唯一值并对其进行计数的新数据框? - How would I go about creating a new data frame that has the unique values of a a column and it counts them? 在 Python 的 dataframe 的行尾添加特定的列值 - Add a specific column values at the end of row in dataframe in Python python:字典键作为行索引,值作为列标题。 如何使用字典引用并选择 df 中的特定值? - python: Dictionary key as row index, values as column headers. How do I refer back and select specific values in a df using a dictionary? 如何在同一行名称内按列插入空白行的值,然后将插入的数据复制到原始 DataFrame? - How do I interpolate values for blank rows column-wise within the same row name, and then copy the interpolated data to the original DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM