Python: how to do basic data manipulation like in R?

前端 未结 4 1111
半阙折子戏
半阙折子戏 2021-01-31 21:22

I have been working with R for several years. R is very strong in data manipulation. I\'m learning python and I would like to know how to manipulate data using python. Basically

4条回答
  •  感情败类
    2021-01-31 21:52

    import csv
    from itertools import izip
    
    with open('source.csv') as f:
        reader = csv.reader(f)
        # filter data
        data = (row for row in reader if row[1].strip() in ('5', '8'))
        # make a new variable
        data = (row + [int(row[2]) * 3] for row in data)
        # transpose data
        data = izip(*data)
        # write data to a new csv file
        with open('destination.csv', 'w') as fw:
            csv.writer(fw).writerows(data)
    

提交回复
热议问题