Python Pandas - Insert list items in order in a column

Asked

Viewed 16 times

0

That’s the kind of doubt that gets hard to even explain, but let’s try.

Follows the code:

data = {"Id": ["01", "02", "03", "04",'05'],"Fruta": ['bananaAAA','bananaBBB','bananaCCC','bananaDDD','bananaEEE']}
base_dados = pd.DataFrame(data)
display(base_Dados)

    Id  Fruta
0   01  bananaAAA
1   02  bananaBBB
2   03  bananaCCC
3   04  bananaDDD
4   05  bananaEEE

This is the dataframe, as you can see, after all "banana" has a type of code, for example: AAA, BBB, CCC... Well, I’ve only taken those points from those items in the code below.

for fruta in base_dados['Fruta']:
    fruta = fruta[6:9]
    print(fruta)

AAA
BBB
CCC
DDD
EEE

So far so good, but I would like to insert a new column in the dataframe(base_data) with this string information, as in the code below

base_dados.insert(2, 'tipo', fruta)
display(base_dados)
    Id  Fruta   tipo
0   01  bananaAAA   EEE
1   02  bananaBBB   EEE
2   03  bananaCCC   EEE
3   04  bananaDDD   EEE
4   05  bananaEEE   EEE

However, as seen, items are not inserted in order. Here’s an example of how I wanted:

    Id  Fruta   tipo
0   01  bananaAAA   AAA
1   02  bananaBBB   BBB
2   03  bananaCCC   CCC
3   04  bananaDDD   DDD
4   05  bananaEEE   EEE

1 answer

0


To do this you must create another column in the datraframe and use the method .apply() to apply a formula for each line.

The code to create the "type" column looks like this:

base_dados['tipo'] = base_dados['Fruta'].apply(lambda x: x[6:9])

Upshot:

Id Fruit guy
0 01 bananaAAA AAA
1 02 bananaBBB BBB
2 03 bananaCCC CCC
3 04 bananaDDD DDD
4 05 bananaEE EEE

Your mistake happened because you were resetting the variable fruta and applying again to all columns.

  • It is already the second time today Philip! Thank you for your attention. So, I believe that the path is right there, when I did in my real project that is my job (I avoid posting with company data), returns the following result: A value is trying to be set on a copy of a Slice from a Dataframe. Try using . Loc[row_indexer,col_indexer] = value Instead So I decided to put .loc... but ai returned the following result: "None of [Index(['500', '195'], dtype='Object')] are in the [index]" 500 and 195 are the results I want to insert.

  • https://ibb.co/mFmtW3K as it turns out

  • Hello, Bruno, it’s a pleasure to help you. Try it like this: mtx['AH'] = list(map(lambda x: x[11:14],mtx['Descricao']))). In my test do not give the same mistake you find.

  • It worked Elipe, Thank you! I created a new dataframe on top of the base_data, followed the article: https://paulovasconcellos.com.br/o-que-%C3%A9-a-value-is-trying-to-be-set-on-a-copy-of-a-Slice-from-a-dataframe-e85f744d8be1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.