1

My csv looks like this:

name;        street;         number;
------------------------------------
Jimmy;       Nice Street 24; 24;
Carl;        Great Street;   128;
Tim;         Long Street 5;   - ;
...

I read that csv with panda like this:

data = pd.read_csv(r'export.csv')
x = data[['name', 'street', 'number']]

As you can see the users did not input their adress correctly in line 1 and 2.

So what I want to do is check each street name for a name and if there is already a number in it. If there is, erase the number from the street row and put it in the number row if there isn't one yet. All lines should look like line 2 later.

I am new to python and pandas and can't figure out the smoothest way to do this. Any input is much appreaciated!

2 Answers 2

1

I would consider removing the trailing separators in a source csv. But it is not necessary.

This code will do the magic:

import pandas as pd
import re


def check_street_no(row):
    number_match = re.search(r'\d+$', row['street'])
    if number_match is not None:
        row['number'] = number_match.group()
        row['street'] = re.sub(r' *\d+$', '', row['street'])
    return row

data = pd.read_csv(r'streets.csv', sep=';', skiprows=[1], skipinitialspace=True)
data = data.apply(check_street_no, axis=1)
print(data)

Output:

    name        street number
0  Jimmy   Nice Street     24
1   Carl  Great Street    128
2    Tim   Long Street      5
Sign up to request clarification or add additional context in comments.

Comments

0

You can use str.extract with combine_first for replace NaNs to original values, for reorder columns use reindex_axis:

df = pd.read_csv(r'export.csv', sep=';', skiprows=[1], skipinitialspace=True)

#if necessary remove columns full of NaNs
df = df.dropna(how='all', axis=1)
df1 = df['street'].str.extract('(?P<street>[a-zA-z\s]+) (?P<number>\d+)', expand=True)
print (df1)
        street number
0  Nice Street     24
1          NaN    NaN
2  Long Street      5

df = df1.combine_first(df).reindex_axis(df.columns, axis=1)
print (df)
    name        street number
0  Jimmy   Nice Street     24
1   Carl  Great Street    128
2    Tim   Long Street      5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.