0

I have csv file, which contains missing column values, how to copy missing column value from previous row ?

product_id,name,modifier
1,test,cheese
,test,eggs
,test,onions
2,test2,cheese
,test2,eggs
,test2,onions

Above CSV data, gets below output

enter image description here

import pandas as pd
import numpy as np
df = pd.read_csv("pivot-products.csv")
df.pivot(index='product_id',columns='modifier') # <-- throws error

Above pivot code works fine with below dataset, where I manually copied it, how to cleanup in pandas to look like below ?

product_id,name,modifier
1,test,cheese
1,test,eggs
1,test,onions
2,test2,cheese
2,test2,eggs
2,test2,onions
ajayramesh
  • 3,576
  • 8
  • 50
  • 75

4 Answers4

2

Try using ffill. For example:

df = pd.read_csv("pivot-products.csv")
df["product_id"] = df["product_id"].ffill()
C. Braun
  • 5,061
  • 19
  • 47
1

Try:

df['product_id'].ffill(inplace=True)
Muhammad Hassan
  • 4,079
  • 1
  • 13
  • 27
1

pandas.fillna

but look specifically for the method='ffill' option

Ian
  • 933
  • 12
  • 17
1
df.fillna(mehtod='ffill').fillna(value)

You do fillna twice because the 'ffill' will fail to fill nan values in the first row. You can define the value.

emremrah
  • 1,733
  • 13
  • 19