-1

I have a dataframe with many rows and columns. One column - 'diagnostic superclass' has labels for every patient stored in rows as lists. It looks like this:

['MI', 'HYP', 'STTC']
['MI', 'CD', 'STTC']

I need to obtain a first label from every row The desired output is a column which stores every first list element of every row so I wrote a function:

def labels(column_with_lists):
    label = column_with_lists
    for a in column_with_lists() :
        list_label = column_with_lists[0]
        label = list_label[0]
    return label

So when I run the code I face the following problem:

Traceback (most recent call last):
  File "C:/Users/nikit/PycharmProjects/ECG/ECG.py", line 77, in <module>
    print(labels(y_true['diagnostic_superclass']))
  File "C:/Users/nikit/PycharmProjects/ECG/ECG.py", line 63, in labels
    for a in column_with_lists() :
TypeError: 'Series' object is not callable
Nike
  • 29
  • 1
  • 8
  • What specifically are you trying to do with `column_with_lists()`? As the error says, that's a `Series`, which can't be called as a function. – Carcigenicate Sep 11 '20 at 19:49
  • Hello! I want to obtain the first value of every row list to create labels – Nike Sep 11 '20 at 19:50
  • Change `column_with_lists()` to `column_with_lists` – Vivek Kalyanarangan Sep 11 '20 at 19:51
  • @VivekKalyanarangan does not work – Nike Sep 11 '20 at 19:53
  • 2
    Please, share the **desired output**. That really helps readers trying to understand what you're achieving – Alexandre B. Sep 11 '20 at 19:54
  • @AlexandreB. updated – Nike Sep 11 '20 at 19:55
  • @Carcigenicate Traceback (most recent call last): `File "C:/Users/nikit/PycharmProjects/ECG/ECG.py", line 77, in print(labels(y_true['diagnostic_superclass'])) File "C:/Users/nikit/PycharmProjects/ECG/ECG.py", line 64, in labels list_label = a[0] IndexError: list index out of range` – Nike Sep 11 '20 at 19:56
  • 1
    @Nike. Please, after taking the [tour](https://stackoverflow.com/tour) (you will have some rewards for that), please have a look at [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Then, edit the question adding a *sample* of your input data and the *desired output* on this sample. – Alexandre B. Sep 11 '20 at 20:04

2 Answers2

2

The error is because of the () after the variable name, which means to to call it as a function. It's a pandas series, not a function.

One way to get a new series from the series of lists is with pandas.Series.apply()

def labels(column_with_lists):
    return column_with_lists.apply(lambda x: x[0])
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

As @Vivek Kalyanarangan said, remove the parenthesis and it will work but I think that you are confuse, why you are iterate in this part if you dont use "a" for anything?

for a in column_with_lists :
    list_label = column_with_lists[0]
    label = list_label[0]

I think that you must storage the first item of each row in a list. In fact, you don't need to use a function:

first_element_of_each_row = [i[0] for i in y_true['diagnostic_superclass'].to_numpy()]

This should be work.

Bricam
  • 71
  • 5
  • I have tried but: `Traceback (most recent call last): File "C:/Users/nikit/PycharmProjects/ECG/ECG.py", line 67, in first_element_of_each_row = [i[0] for i in y_true['diagnostic_superclass'].to_numpy()] File "C:/Users/nikit/PycharmProjects/ECG/ECG.py", line 67, in first_element_of_each_row = [i[0] for i in y_true['diagnostic_superclass'].to_numpy()] IndexError: list index out of range ` – Nike Sep 11 '20 at 20:26