简体   繁体   中英

Adding a number to a column with python

I am very new to python and I would be grateful for some guidance with the following. I have a text file with over 5 million rows and 8 columns, I am trying to add "15" to each value in column 4 only.

For example:

  10  21  34  12  50  111  234  21  7
  21  10  23  56  80   90  221  78 90

Would be changed to:

  10  21  34  12  **65**  111  234  21  7
  21  10  23  56  **95**   90  221  78 90

My script below allows me to isolate the column, but when I try to add any amount to it i return "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'"

file = open("file.txt")
column = []

for line in file:
    column.append(int(line.split("\t")[3]))

print column

Any advice would be great.

try this to get you started -- there are many better ways using libraries but this will show you some better file handling basic methods anyway. works for the data you posted -- as long as the delimiter in your files is double space (" ") and that everything can be cast to an int. If not.....

Also -- note the correct way to start a script is with:

if __name__ == "__main__":

this is because you wont generally want any code to execute if you are making a library...

__author__ = 'charlie'

in_filename = "in_file.txt"
out_filename = "out_file.txt"
delimiter = "  "

def main():

    with open(in_filename, "r") as infile:
        with open(out_filename, "w") as outfile:
            for line in infile:

                ldata = line.split(delimiter)

                ldata[4] = str(int(ldata[4]) + 15)

                outfile.write(delimiter.join(ldata))


if __name__ == "__main__":
    main()

With Pandas :

import pandas as pd

df = pd.read_clipboard(header=None)
df[4] += 15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM