1

I have such problem: DataFrame for this:http://sigmaquality.pl/wp-content/uploads/2019/03/sample.csv

enter image description here

I have two columns: postal code and code of country. I have many null cells in column: code of country.

I know if postal code has mask XX-XXX this is Polish code. Because I know it, I can fill empty cells by symbol: 'PL'. I don't know how to do it.

How to use condition to fill data?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wojciech Moszczyński
  • 2,893
  • 21
  • 27

5 Answers5

5

Use groupby and ffill() with bfill():

df.groupby('POSTAL_CD').apply(lambda x: x.ffill().bfill())

   Unnamed: 0 POSTAL_CD COUNTRY
0         0.0    33-101      PL
1         1.0    277 32      CZ
2         2.0    72-010      PL
3         3.0    33-101      PL
4         4.0      7700      BE
5         5.0    72-010      PL
6         6.0    33-101      PL
7         7.0     10095      IT
8         8.0    33-101      PL
9         9.0    33-101      PL
anky
  • 74,114
  • 11
  • 41
  • 70
5

Check with np.where with str.contains

df['COUNTRY']=np.where(df['POSTAL_CD'].str.match(r'\d{2}-\d{3}')&df['COUNTRY'].isnull(),'PL',df['COUNTRY'])
BENY
  • 317,841
  • 20
  • 164
  • 234
3

How about using the loc indexer as shown here.

df = pd.read_csv("sample.csv", sep=",", index_col=0)
df.loc[df["POSTAL_CD"].str.contains("-", na=False), "COUNTRY"] = "PL"
jsgounot
  • 709
  • 6
  • 12
2

When I wrote this code, I considered that you need a mask with [two digits]-[three digits] for postal codes, not just having a dash inside or non-empty field.

import re
import csv

# Compile our regexp
regexp = re.compile(r'[0-9]{2}-[0-9]{3}')

# Read the CSV and load it into memory
reader = csv.DictReader(open('sample.csv'))
table = list(reader)

# Iterate for rows
for row in table:
    # Check if the postal code is fit to our regexp
    if regexp.match(row['POSTAL_CD']):
        row['COUNTRY'] = 'PL'

# Write the result
with open('result.csv', 'w') as f:
    writer = csv.DictWriter(f, fieldnames=['', 'POSTAL_CD', 'COUNTRY'])
    writer.writeheader()
    writer.writerows(table)
vurmux
  • 9,420
  • 3
  • 25
  • 45
1

After a while I learned a little and I would do this:

df['Nowa'] = df['POSTAL_CD'].str.slice(2,3)
df['Nowa'] = df['Nowa'].apply(lambda x: 'PL' if x == '-' else np.nan)
df['COUNTRY'].fillna(df['Nowa'], inplace=True) 
del df['Nowa']
df

enter image description here

Wojciech Moszczyński
  • 2,893
  • 21
  • 27