循环所有列以获取任何列中的值

时间:2019-07-31 20:44:57

标签: python pandas

我试图遍历数据帧中的所有列,以查找满足“功能”条件的位置,以便更改FeatureValue。因此,如果我的dataframe(df)如下所示:

RoutingModel routing = new RoutingModel(manager);
routing.AddConstantDimension(1, 4, false, "maxNodesToVisit");

我想找到Feature = Cat或Feature2 = Cat的位置,并将FeatureValue和Feature2Value更改为20。我尝试了以下内容,但仍在努力之中。

Feature    FeatureValue    Feature2    Feature2Value
    Cat               1         Dog                3
    Fish              2         Cat                1

解决方案如下:

for column in df:
    if df.loc[df[column] == "Cat"]:
print(column)

2 个答案:

答案 0 :(得分:2)

这是一种实现方法:

# First we construct a dictionary linking each feature to its value column
feature_value = {'Feature' : 'FeatureValue', 'Feature2' : 'Feature2Value'}

# We iterate over each feature column
for feature in feature_value:
    df.loc[df[feature]=='Cat', feature_value[feature]] = 20

答案 1 :(得分:0)

您当前拥有广泛的数据结构。为了以一种优雅的方式解决您的问题,您应该转换为一个长数据结构。我不知道您要如何处理数据,但是长格式通常更容易处理。

您可以这样做

import pandas as np
from itertools import chain

# set up your sample data
dta = {'Feature': ['Cat', 'Fish'], 'FeatureValue': [1, 2], 'Feature2': ['Dog', 'Cat'], 'Feature2Value': [3, 1]}
df = pd.DataFrame(data=dta)

# relabel your columns to be able to apply method `wide_to_long`
# this is a little ugly here only because your column labels are not wisely chosen
# if you had [Feature1,FeatureValue1,Feature2,FeatureValue2] as column labels,
# you could get rid of this part
columns = ['Feature', 'FeatureValue'] * int(len(df.columns)/2)
identifier = zip(range(int(len(df.columns)/2)), range(int(len(df.columns)/2)))
identifier = list(chain(*identifier))
columns = ['{}{}'.format(i,j) for i, j in zip(columns, identifier)] 
df.columns = columns

# generate result
df['feature'] = df.index
df_long = pd.wide_to_long(df, stubnames=['Feature', 'FeatureValue'], i='feature', j='id')

现在,您将数据转换自

  Feature  FeatureValue Feature2  Feature2Value
0     Cat             1      Dog              3
1    Fish             2      Cat              1

对此

           Feature  FeatureValue
feature id                      
0       0      Cat             1
1       0     Fish             2
0       1      Dog             3
1       1      Cat             1

这使您可以单行回答问题,没有循环:

df_long.loc[df_long['Feature'] == 'Cat', 'FeatureValue'] = 20

这产生

           Feature  FeatureValue
feature id                      
0       0      Cat            20
1       0     Fish             2
0       1      Dog             3
1       1      Cat            20

您可以使用相同的方法轻松地返回宽屏格式。