Pandas Data Modification, Iteration, and Function Application Techniques
This article provides a comprehensive guide to using Pandas for data cleaning and transformation, covering value modification, replacement, filling missing data, renaming, column addition, row insertion, merging, deletion, advanced filtering, iteration methods, and applying functions such as pipe, apply, agg, and transform.
During data analysis and modeling, Pandas is frequently used to modify, add, or delete fields; this guide introduces common operations and their syntax.
Value modification examples include accessing cells with df.iloc[0,0] , updating values, and bulk changes like df[df.Q1 < 60] = 60 .
Replacement can be performed with df.replace(0, 5) or dictionary mappings such as df.replace({'Q1': 0}, 100) .
Filling missing values uses df.fillna(0) , method options like df.fillna(method='ffill') , or column‑specific dictionaries.
Renaming columns and indexes is done via df.rename(columns={'team':'class'}) and df.rename_axis('animal') .
Adding columns can be achieved with direct assignment df['foo'] = 100 , calculations df['total'] = df.select_dtypes(include=['int']).sum(1) , or df.insert(2, 'total', df.sum(1)) . The df.assign() method allows chaining multiple new columns.
Evaluating expressions uses df.eval('total = Q1+Q2+Q3') and can incorporate variables.
Appending rows is demonstrated with df.loc[100] = ['tom', 'A', 88, 88, 88, 88] and df.append(df2) .
Deletion examples include s.pop(3) and df.dropna() with various parameters.
Advanced filtering shows df.where(df > 70) , np.where(df >= 60, '合格', '不及格') , and df.mask(s > 80) .
Iteration methods covered are for i in df.name , df.iterrows() , df.itertuples() , df.items() , and column iteration for column in df .
Function application includes df.pipe() chaining, df.apply(lambda x: x.lower()) , df.applymap() , df.map() , aggregation with df.agg('max') , transformation via df.transform(lambda x: x*2) , and copying with s.copy() .
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.