itemName
2020
2021
2022
2023
2024
год
item1
5
20
10
10
50
3
item2
10
10
50
20
40
2
item3
12
35
73
10
54
4
Результат должен быть таким:
itemName
2020
2021
2022
2023
2024
год
year_name
item1
5
20
10
10
50
3
2022
item2
10
10
50
20
40
2
2021
item3
12
35
73
10
54
4
2023
Я пытаюсь использовать лямбда-функцию, чтобы использовать значение каждой строки «год», и использовать ее, чтобы найти имя столбца для этой строки и добавить его в столбец «имя_года».
Я пробовал:
Код: Выделить всё
col_names = result_dict[col].columns.tolist()
result_df[[last_year_header']] = result_df[[_last_year']].apply(lambda x: col_names[x])
Код: Выделить всё
TypeError: list indices must be integers or slices, not Series
Код: Выделить всё
col_names = result_dict[col].columns.tolist()
result_df[[last_year_header']] = result_df[[_last_year']].apply(lambda x: col_names[x.iloc[0].astype(int)])
Код: Выделить всё
IndexError: list index out of range
Подробнее здесь: https://stackoverflow.com/questions/797 ... -in-pandas
Мобильная версия