0

So I have this function:

def get_tag(word):
    df1 = pd.DataFrame(0, index=[1], columns=cols)
    df2 = df1
    df1 = get_freqs(word)
    L = df1.loc[1, :].idxmax()
    if df1.loc[1, L] > 0:
        df2.loc[1, L] = 1
    return df2

which returns a Series, as I expect it to :

wf.get_tag("email")
   City  CompanyName  Country   ...    MiddleName  State  Street
1     0            0        0   ...             1      0       0
[1 rows x 10 columns]

yet if I use it and assign the result to a cell of another series the value is nan:

word_df = pd.DataFrame(columns=WORD_FEATS)
...
word_df.loc[0, "TAGS"] = wf.get_tag(word)

By printing:

for l in list(word_df):
    print(l + ": " + str(word_df.loc[0, l]))

I get :

TAGS: nan

for any input.

Why does this happen?

Makis Kans
  • 337
  • 3
  • 12
  • Why do you think `wf.get_tag("email")` is a series? Looks like a shape `(1, 10)` *dataframe*. In addition, what is `word_df` and what is your desired output in `word_df`? Can you show us rather than describing? See also **[mcve]**. – jpp Oct 23 '18 at 09:37
  • Solved with dataframe.at as here : https://stackoverflow.com/questions/26483254/python-pandas-insert-list-into-a-cell – Makis Kans Oct 23 '18 at 09:39
  • In general, you [shouldn't put lists in series](https://meta.stackoverflow.com/questions/373714/generic-dont-do-it-answer). – jpp Oct 23 '18 at 09:41
  • You can *want* anything you like. Just don't expect it to go as planned. Pandas stands for panel data, that means an X-Y grid of row labels and column labels. Just because putting arbitrary objects in series works doesn't mean it's a *good idea*. – jpp Oct 23 '18 at 09:43

0 Answers0