PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to append the last 4 letters of every item in a dataframe column to a list

 May 12, 2022     append, list, python     No comments   

Issue

I have an inventory system with two separate SKUs in a single string, example (FT3M-FL3M), I want to make a list called "first_item" and append the first 4 letters (first SKU) to it and do this for every item in the column. Then do the same but with taking the last 4 digits of each string (second SKU)

This is the example dataframe

| Index    | Skus           |
| -------- | -------------- |
| First    | FT3M-FL3M      |
| Second   | FT2M-FL2M      |
| Second   | FT1M-FB1M      |
| Second   | FT4S-FL4S      |
| Second   | FT4S-FB2M      |

This is the code I have:

first_list = []
for i in sku:
first_list.append(i[0:4])
print(first_list)

It clearly doesn't work and I get a massive list.


Solution

If you need a list of first / last n characters of a string inside a pandas column you can try:

# first
my_first = [x[:4] for x in df.wanted_column]

# last
my_last = [x[-4:] for x in df.wanted_column]

This gives you a possibility to filter the list on the fly if needed, for example:

my_filtered = my_first = [x[:4] for x in df.wanted_column if 'some_sku' not in x]


Answered By - Jonas Palačionis
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing