Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill empty values from a row with the value of next column on the same row on csv file with pandas

Tags:

python

pandas

csv

I have this type of DataFrame

name     surname       middle

Frank    Doe           NaN
John     Nan           Wood
Jack     Putt          Nan
Frank    Nan           Joyce

I want to move "middle" values on NaN same rows values on "surname" column. How can i do this? I tried to use the fillna method but got no results. Here is my code:

import os
from pandas.io.parsers import read_csv


for csvFilename in os.listdir('.'):
   if not csvFilename.endswith('.csv'):
      continue
data=read_csv(csvFilename)
filtered_data["surname"].fillna(filtered_data["middle"].mean(),inplace=True)
filtered_data.to_csv('output.csv' , index=False)
like image 480
tafazzi87 Avatar asked Dec 04 '25 09:12

tafazzi87


1 Answers

Conditional column flipping

Using pd.isnull(), columns can be rearranged conditionally.

import pandas as pd
from cStringIO import StringIO

# Create fake DataFrame... you can read this in however you like
df = pd.read_table(StringIO('''
name     surname       middle
Frank    Doe           NaN
John     NaN           Wood
Jack     Putt          NaN
Frank    NaN           Joyce'''), sep='\s+')

print 'Original DataFrame:'
print df
print

# Assign the middle name to any surname with a NaN
df.loc[pd.isnull(df['surname']), 'surname'] = df[pd.isnull(df['surname'])]['middle']

print 'Manipulated DataFrame:'
print df
print

Original DataFrame:
    name surname middle
0  Frank     Doe    NaN
1   John     NaN   Wood
2   Jack    Putt    NaN
3  Frank     NaN  Joyce

Manipulated DataFrame:
    name surname middle
0  Frank     Doe    NaN
1   John    Wood   Wood
2   Jack    Putt    NaN
3  Frank   Joyce  Joyce
like image 193
tmthydvnprt Avatar answered Dec 06 '25 22:12

tmthydvnprt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!