How to create a new csv file with only modified datas?(Pandas)

I want to create a new csv file with only modified datas. The input csv file is of example as enter image description here

input.csv

Now I want to create a new csv file of modified datas only like

output.csv

The condition is :

df1.loc[df1['Code'].astype(str).str.len() >12, 'Code']= '0'

What I've tried is

import pandas as pd

df1 = pd.read_csv('input.csv')
df1.loc[df1['Code'].astype(str).str.len() >12, 'Code']= ''


df2 = df1.reindex(['ID','Name','Code','Changed Code'],axis=1)

How to create a new dataframe for modified datas in this case? Any suggestions or ideas?

2 answers

  • answered 2021-01-11 05:31 jezrael

    You can change your code for select rows by condition with columns names in list and assign to new DataFrame df2, also you can add dtype parameter to read_csv for avoid converting column Code to numbers (and adding scientific notation):

    df1 = pd.read_csv('input.csv', dtype={'Code':str})
    
    #if need new column filled by 0
    df1['Changed Code'] = '0'
    df2 = df1.loc[df1['Code'].str.len() >12, ['ID','Name','Code','Changed Code']]
    

    If converting to strings in read_csv is not necessary:

     df1 = pd.read_csv('input.csv')
     df2 = df1.loc[df1['Code'].astype(str).str.len() >12, ['ID','Name','Code','Changed Code']]
     df2['Changed Code'] = '0'
    

  • answered 2021-01-11 06:06 Atom Store

    This solved my problem.

    import pandas as pd
    
    df1 = pd.read_csv('input.csv')
    
    
    df2 = df1.loc[df1['Code'].astype(str).str.len() >12]
    df2 = df2.reindex(['Id','Name','Code','Changed Code'],axis=1)
    
    df2.loc[df2['Code'].astype(str).str.len() >12,'Changed Code']='0'
    df2