Home > Blockchain >  Pop the first element in a pandas column
Pop the first element in a pandas column

Time:10-19

I have a pandas column like below:

import pandas as pd

data = {'id':  ['001', '002', '003'],
        'address': [['William J. Clare', '290 Valley Dr.', 'Casper, WY 82604','USA, United States'],
                    ['1180 Shelard Tower', 'Minneapolis, MN 55426', 'USA, United States'],
                    ['William N. Barnard', '145 S. Durbin', 'Casper, WY 82601', 'USA, United States']]
        }

df = pd.DataFrame(data)

I wanted to pop the 1st element in the address column list if its name or if it doesn't contain any number.

output:

[['290 Valley Dr.', 'Casper, WY 82604','USA, United States'], ['1180 Shelard Tower', 'Minneapolis, MN 55426', 'USA, United States'], ['145 S. Durbin', 'Casper, WY 82601', 'USA, United States']]

This is continuation of my previous post. I am learning python and this is my 2nd project and I am struggling with this from morning please help me.

CodePudding user response:

Assuming you define an address as a string starting with a number (you can change the logic):

for l in df['address']:
    if not l[0][0].isdigit():
        l.pop(0)

print(df)

updated df:

    id                                            address
0  001  [290 Valley Dr., Casper, WY 82604, USA, United...
1  002  [1180 Shelard Tower, Minneapolis, MN 55426, US...
2  003  [145 S. Durbin, Casper, WY 82601, USA, United ...
  • Related