[英]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.