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

前端 未结 4 1109
半阙折子戏
半阙折子戏 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:53

    There's a module for CSV parsing in the standard library. To get a list of row each containing a list of the cells, you can use list(csv.reader(...)).

    Step 2 and 3 can be written in one list comprehension: [(var1, var2, var3, var3 * 3) for var1, var2, var3 in data if var2 in ('5', '8')].

    I'm not aware of anything in the standard library for transposing lists of lists. Perhaps NumPy or SciPy has something. A quick and dirty way would be zipping the rows (zip(*lists)).

    Writing back to file should be as simple as constructing as csv.writer and passing it each row in a loop.

提交回复
热议问题