Как сопоставить заголовок столбца с содержимым столбцов и изменить значения? - PullRequest
0 голосов
/ 14 января 2020

У меня есть фрейм данных, подобный следующему:

enter image description here

Мне нужно сохранить некоторые значения, соответствующие заголовку столбца, который содержит один дополнительный столбец.

Не могли бы вы предложить решение?

1 Ответ

0 голосов
/ 14 января 2020

Вот решение:

# Building of a sample dataframe
df = pd.DataFrame({"index":["B","D","C","A"]}).groupby(["index"]).count()
df["value"] = None
# Function to fill the matching column
def match(index, column):
    if(index==column):
        return 1
    else:
        return ""
# Create the matching column and fill it with the right value
for index in df.index.array:
    df[index] = df.apply(lambda row: match(row.name, index), axis=1)
# Print the result dataframe
print(df)
...