添加从现有列计算的ASCII列

问题描述 投票:0回答:1

我有一个包含两列的ascii文件。我需要再添加两列。输出文本文件应包含这两列,并且括号中的原始两列用逗号分隔。在gedit中打开ascii,我的输入文件如下所示:

1  2
3  4
5  6
7  8

最后我希望它是这样的:

2 6 (1,2)
6 12 (3,4)
10 18 (5,6)
14 24 (7,8)

所以我的两个新列是原始列的两个/三个的倍数。我只是在文件中读取大熊猫数据框并且已经混淆了

import pandas as pd

df = pd.read_csv("test.txt")
print(df)

       1  2
0      3  4
1      5  6
2      7  8

我想输出到ascii的pandas数据帧应该具有以下结构:

     2 6 (1   2)
0    6 12 (3   4)
1    10 18 (5   6)
2    14 24 (7   8)

我甚至不知道如何开始因为我完全不了解结构等任何帮助表示赞赏!

python ascii calculated-columns
1个回答
0
投票

你可以在没有熊猫的情况下阅读文件:

we=open('new.txt','w')
with open('read.txt') as f:
    for line in f:
#read a line
         a,b=line.split('\t')
#get two values as string
         c=2*int(a)
         d=3*int(b)
#calculate the other two values
         ### edited
         e = ''.join(("(",str(a),",",str(b),")"))
         print(e)
         ####

         #e=str(tuple(a,b))
         #we.write(str(c)+' '+str(d)+e+'\n'
#write to new file

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.