how to filter or manipulate columns of csv file using another column using python

gotam
import csv
from datetime import datetime
import random

f=open("gh.csv","w")
csv.reader(f)
g=open("gh.csv","a")
k=csv.writer(g)
k.writerow(("age","ageband","gender","disease"))
for i in range(10):
    k.writerow((random.choice((range(0,100))),'40-50',random.choice(['male','female']),random.choice(['heartstroke','breastcancer','hypertension','hive'])))
for col in f:
    age=row[2]
    if 40<=age>=55:
      print '40-55'
    elif 56<=age>=75:
      print '56-75'
    elif 76<=age>=100:
      print '76-100'
    else :
      print 'age>20'    
f.close()


age ageband gender  disease
62  40-50   female  breastcancer
15  40-50   female  breastcancer
6   40-50   male    breastcancer
59  40-50   male    heartstroke
64  40-50   female  breastcancer
98  40-50   female  breastcancer
51  40-50   female  heartstroke
95  40-50   male    heartstroke
27  40-50   male    breastcancer
31  40-50   male    hypertension

after writing random data into csv file . is it possible to overwrite certain fields based on columns. for example if the age is 35 then the age band column should be updated as ''56-75'. and breast cancer should be updated only for females. please i need help as i am new to python. any kind of help is much appreciated

rescdsk

A couple of options are csvkit and pandas (which is a much bigger computing library, but should have good support for this sort of thing).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to filter rows between two dates from CSV file using python and redirect to another file?

How to add columns in a .csv using existing data in another column?

how to filter a .csv/.txt file using a list from another .txt

How to manipulate data in a .csv file using Pandas and access specific row and column?

How to filter based on id which contains specific name in another column in an excel file using Python

How to filter string from all column from csv file using python

How to append another column header and values to CSV file using Pandas in Python

Comparing multiple columns of same CSV file and returning result to another CSV file using Python

Filter rows based on multiple columns then manipulate another column in R

How can I filter keywords from a csv file using Python

How to write values to CSV file in two columns using Python 3?

How to display columns in CSV file in python using Pandas?

how to append a column from a csv file to another csv file without using panda?

Read CSV file, manipulate columns and append result in new column. Python 2.7

Manipulate specific columns (sample features) conditional on another column's entries (feature value) using pandas/numpy dataframe

Manipulate CSV file using groovy and java

How to create a derived column in a CSV file using linux/python?

How to retrieve one column from csv file using python?

How to group a column in a csv file by a range and plot histogram using python?

how to access each column in csv file using python

How to overwrite a particular column of a csv file using pandas or normal python?

How to find column of same values in csv file using python

How to append a new column to a CSV file using Python?

How to insert a column at the beginning of csv file using python

How can i filter fast the huge csv file based on list of ids from another file using bash?

Split values for columns in CSV file using Python

Parsing a CSV file into columns (preferably using python)

Compare columns of a CSV file using Python

Unique elements in columns in csv file using python

TOP Ranking

HotTag

Archive