简体   繁体   English

pandas获取包含值的列的名称

[英]pandas get the name of the column that contains a value

I am working on a script and using the pandas lib. 我正在编写一个脚本并使用pandas lib。 I am new to the pandas lib so the question may be silly. 我是熊猫的新手,所以问题可能很愚蠢。 I've imported my data from a csv into a pandas.dataframe . 我已将数据从csv导入到pandas.dataframe My data frame looks like below: 我的数据框如下所示:

                      set1            set2               set3      set4  
0                     744110.0        507121.0           790001.0  785693.0   
1                     744107.0        507126.0           791002.0  788107.0   
2                     744208.0        535214.0           791103.0  788108.0   
3                     744210.0        534195.0           790116.0  784170.0

I am facing 2 problems: 我面临两个问题:

Problem 1 问题1

The values in the csv are integer I don't know why or how is that .0 popping up, I don't want that to happen. csv中的值是整数我不知道为什么或如何.0弹出,我不希望发生这种情况。

I create my dataFrame with the below code line: 我使用以下代码行创建我的dataFrame

df = pd.read_csv(file_path)

Problem 2 问题2

I want to do a search through the sets and get the name of the set that contains a value, for example: if I pass in the value 791103 the output should be name set3 as a string. 我想在集合中搜索并获取包含值的集合的名称,例如:如果我传入值791103则输出应该是名称set3作为字符串。

How can I achieve this in pandas 我怎样才能在熊猫中实现这一目标

Please Note: different columns may have different number of items for example, set1 may have 500 total values while, set2 may just have 40 请注意:不同的列可能具有不同数量的项目,例如,set1可能有500个总值,而set2可能只有40个

.to_dict('list') output: .to_dict('list')输出:

{'set1': [744110.0, 744107.0, 744208.0, 744210.0], 'set2': [507121.0, 507126.0, 535214.0, 534195.0], 'set3': [790001.0, 791002.0, 791103.0, 790116.0], 'set4': [785693.0, 788107.0, 788108.0, 788170.0]}

import numpy as np
import pandas as pd

"""  set1  set2  set3  set4  
0  744110.0  507121.0  790001.0  785693.0
1  744107.0  507126.0  791002.0  788107.0
2  744208.0  535214.0  791103.0  788108.0
3  744210.0  534195.0  790116.0  784170.0
"""
df = pd.read_clipboard(sep='\s{2,}', engine='python', dtype = 'int')
df

For your first problem, you can set the data types upon import. 对于第一个问题,您可以在导入时设置数据类型。 As mentioned by @user32185, NaN s can cause issues when trying to cast as int. 正如@ user32185所提到的, NaN可能会在尝试转换为int时导致问题。

pd.read_csv(filename, dtype = 'int')

For your second, I tried a couple of things, but this worked best: 对于你的第二个,我尝试了几件事,但这最好:

import numpy as np
df.iloc[np.where(df == 791103)]

Output: 输出:

    set3
2   791103

To get just the column name: 要获得列名称:

df.iloc[np.where(df == 791103)].columns[0]

Output: 输出:

'set3'

Links: 链接:

Convert Pandas column containing NaNs to dtype `int` 将包含NaN的Pandas列转换为dtype`int`

https://chrisalbon.com/python/data_wrangling/pandas_create_column_using_conditional/ https://chrisalbon.com/python/data_wrangling/pandas_create_column_using_conditional/

Another option to get the columns with the given value is 获取具有给定value的列的另一个选项是

value = 791103
l = (df.values==value).any(axis=0)
cols = [df.columns[idx] for idx in np.where(l==True)[0]]

On my machine this takes 15.9 µs ± 645 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) while Evans's answer takes 628 µs ± 2.01 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 在我的机器上, 15.9 µs ± 645 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)需要15.9 µs ± 645 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)而Evans的答案628 µs ± 2.01 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)需要628 µs ± 2.01 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

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

相关问题 获取在熊猫中包含特定值的列名 - get column name that contains a specific value in pandas Pandas 获取包含另一列值周围括号的列的列标题名称 - Pandas get column header name for column which contains bracket around value of another column 获取列名,其中包含python pandas中任何行的特定值 - Get column name which contains a specific value at any rows in python pandas 如何在整个Pandas数据帧中搜索字符串,并获取包含该字符串的列的名称? - How to search entire Pandas dataframe for a string and get the name of the column that contains it? 如何获取包含列表或值的列熊猫的唯一值? - how to get the unique value of a column pandas that contains list or value? 如何获得“No”列的最大值。 关于 pandas 中的“名称”列 - How to get maximum value of column "No." with respect to column "Name" in pandas 包含最大值的列的名称 - name of column, that contains the max value 在 Pandas 中查找包含另一列行中特定值的列名 - Find column name in Pandas that contains a specific value in the row from another column 如何将列添加到包含列值在熊猫中对应的组的名称的 DataFrame - How to add a column to a DataFrame that contains the name of the group to which a column value corresponds in pandas Pandas:如何在没有列名和索引的情况下获取值的位置 - Pandas: How to get the location of value without column name and index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM