简体   繁体   English

循环并将数据保存到同一个表中

[英]Looping and saving data into the same table

I am looping through 5 data frames (space separated) and doing some calculations or modifications.我正在遍历 5 个数据帧(空格分隔)并进行一些计算或修改。 After the calculation is done on the first table I would like to save results into the same table by overwriting it and repeat this for all 5 data frames.在第一个表上完成计算后,我想通过覆盖它并将结果保存到同一个表中,并对所有 5 个数据帧重复此操作。

At the moment I am know how to write the first part:目前我知道如何写第一部分:

table = sorted(glob.glob('*.dat'))

for i in table:
    data = pd.read_table(i, delim_whitespace=True, header=None, names=range(5))
    here I am doing some calculation
    and here I would like to save data into the same table by overwriting (but I do not know how)

Any advice or help is more than welcome.我们非常欢迎任何建议或帮助。

Thanks.谢谢。

This is straight forward.这是直截了当的。 Let me know if you are looking for anything else.如果您正在寻找其他任何东西,请告诉我。

# sample dataframes
d1 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]})
d2 = pd.DataFrame({'one' : [5., 6., 7., 8.], 'two' : [9., 10., 11., 12.]})
d3 = pd.DataFrame({'one' : [15., 16., 17., 18.], 'two' : [19., 10., 11., 12.]})

# list of dataframes
mydfs = [d1, d2, d3]

for i in mydfs:
    i['three'] = i['one'] + i['two']
    print(i)

If you check the dataframes after running the above code, you will see each of the dataframes will have column 'three'如果您在运行上述代码后检查数据帧,您将看到每个数据帧都有“三”列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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