0

I have a pandas series like so:

df = pd.Series(
    [['abc', 'xyz1'],
    ['abc', 'xyz1'],
    ['def', 'xyz1'],
    ['def', 'xyz1']]
)

I'd like to convert so that there are no more lists, and only the first entry from each list:

df = pd.Series('abc', 'abc', 'def', 'def')

Any ideas?

Actually this was the result of a dataframe:

temp = df_original["Col1"].str.split("/", n=1)

I wanted in this line to already split and extract the first string (I tried [0] at the end - didn't work). Then I tried:

temp_1 = temp.map(lambda x: x[0])

But still didn't work.

pymat
  • 1,090
  • 1
  • 23
  • 45
  • Because already splitted column use `print (df.map(lambda x: x[0]))` or `print (df.str[0])` – jezrael Mar 17 '20 at 14:42
  • @jezrael I tried this link and still got an error: TypeError: 'float' object is not subscriptable: I saw the other post, that's why I did my post. – pymat Mar 17 '20 at 14:49
  • it means there are some missing values, so need `print (df.str[0]) ` – jezrael Mar 17 '20 at 14:50
  • 1
    @jezrael Actually the .str[0] at the end did the trick, thanks! – pymat Mar 17 '20 at 15:18

0 Answers0