如何迭代行并追加列名

时间:2015-02-26 16:40:54

标签: python pandas dataframe

我有一个数据框(csv文件),它看起来像:

Name   Apple  Banana  Carrot Mango

Adam   false   false  true   true
James  false   true   true   false
Jessie false   false  true   true

我需要遍历行并使用' true'附加列名称。带有输出的字符串中的值:

Carrot,Mango
Banana,Carrot
Carrot,Mango

这是我试图做的事情(这是行不通的):

data = pandas.read_csv("filename.csv")
df = pandas.DataFrame(data)
columns = df.columns.values
str = ""
for index, row in df.iterrows():
    for c in columns:
        if c == True:
            str.append(c)

2 个答案:

答案 0 :(得分:2)

这是使用Pandas的解决方案。

In [1]: import pandas as pd

In [2]: data = pd.DataFrame({"Apple": [False, False, False], "Banana": [False, True, False], "Carrot": [True, True, True], "Mango": [True, False, True]}, columns=["Apple", "Banana", "Carrot", "Mango"])

In [3]: true_columns = []

In [4]: for index, row in data.iterrows():
   ...:     true_col_list = [col for col in data.columns if row[col]]
   ...:     true_columns.append(",".join(true_col_list))
   ...:

In [5]: data["true_columns"] = true_columns

In [6]: data
Out[6]:
   Apple Banana Carrot  Mango   true_columns
0  False  False   True   True   Carrot,Mango
1  False   True   True  False  Banana,Carrot
2  False  False   True   True   Carrot,Mango

答案 1 :(得分:1)

我不知道你真的需要这个pandas

with open("my.csv") as f:
    headers = next(f).split(",")[1:]
    lines = (line for line in f if line.strip())        
    for line in lines:
        for hdr,itm in zip(headers,line.split(",")[1:]:
           if "true" in itm:
               print hdr