У меня есть фрейм данных, подобный следующему:
Мне нужно сохранить некоторые значения, соответствующие заголовку столбца, который содержит один дополнительный столбец.
Не могли бы вы предложить решение?
Вот решение:
# 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)