Загадка: мне нужно использовать df.apply со строкой данных из df, но я не знаю имен возвращаемых столбцов во время вызова df.apply, а имена столбцов зависят от строки входные данные для df.apply ...
Существуют и другие обсуждения стекопотока о pd.concat и result_type = 'expand', но они не совсем подходят для моих целей.
Пожалуйста ответьте простой, как можно строчками, примените стратегию ..
import pandas as pd
def my_func(x):
# get_dictionary call will return a consistent number of key/values pairs
# with the keys being consistent each call.
# e.g. {'key1': [5], 'key2': [5.6]}
dict_of_vals = get_dictionary(x.name, x.some_other_value)
# create a Series with the values - ONE row # can't name columns here??
return pd.Series( list(dict_of_vals.values()) )
# assume df is init with some number of columns/rows,
# see below for example
df = init()
# I need the keys from the my_func call e.g. dict_of_vals.keys()
# for the column names here
df[DONT_KNOW_KEYS_UNTIL_CALL_TO_MY_FUNC] = df.apply(lambda x: my_func(x), axis=1)
# assume initial table from init call above
numbers colors
0 1 red
1 2 white
2 3 blue
# want solution to look something like this:
numbers colors key1 key2
0 1 red 5 5.6
1 2 white 6 7.7
2 3 blue 7 8.8
Again, I won't know the column names and values until the apply
calls my_func.
example, if
dict_of_vals = {'key1': [5], 'key2': [5.6]}
I get
key1 key2
0 [5] [5.6]
1 [5] [5.6]
2 [5] [5.6]
which is missing the original columns