There is a command question that sums up the 2-column txt file.

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

Hello, I don't know much about the programming language, so I don't know if it's the right question, but I'm posting it because I have no questions.

As shown in the figure above, the DOS column in the three left files is the sum of PDOS x, y, and z. I'd like to create a code that adds only the values of the DOS column and takes the E(eV) column as it is in the various files that look like this and makes the same file as the rightmost one. Are there any commands that I can use in Windows for this way?

sum window

2022-09-20 12:34

1 Answers

You can do it with Python Pandas.

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pandas as pd

>>> s1 = """E  DOS   PDOC_x
1    5    1
2    2    1
3    3    1
4    3    2"""
>>> from io import StringIO
>>> s2 = """E  DOS   PDOC_x
1    6    2
2    4    1
3    1    0
4    1    1"""
>>> s3 = """E  DOS   PDOC_x
1    3    2
2    3    1
3    4    3
4    4    1"""


>>> df1 = pd.read_csv(StringIO(s1), delim_whitespace=True)
>>> df1
   E  DOS  PDOC_x
0  1    5       1
1  2    2       1
2  3    3       1
3  4    3       2
>>> df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
 #   #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   E       4 non-null      int64
 1   DOS     4 non-null      int64
 2   PDOC_x  4 non-null      int64
dtypes: int64(3)
memory usage: 224.0 bytes
>>> df2 = pd.read_csv(StringIO(s2), delim_whitespace=True)
>>> df3 = pd.read_csv(StringIO(s3), delim_whitespace=True)
>>> df2
   E  DOS  PDOC_x
0  1    6       2
1  2    4       1
2  3    1       0
3  4    1       1
>>> df3
   E  DOS  PDOC_x
0  1    3       2
1  2    3       1
2  3    4       3
3  4    4       1
>>> df1["DOS"] + df2["DOS"] + df3["DOS"]
0    14
1     9
2     8
3     8
Name: DOS, dtype: int64
>>> df1 = df1.set_index("E")
>>> df2 = df2.set_index("E")
>>> df3 = df3.set_index("E")
>>> df1["DOS"] + df2["DOS"] + df3["DOS"]
E
1    14
2     9
3     8
4     8
Name: DOS, dtype: int64
>>> df4 = pd.DataFrame()
>>> df4["DOC"] = df1["DOS"] + df2["DOS"] + df3["DOS"]
>>> df4
   DOC
E     
1   14
2    9
3    8
4    8
>>> df4.reset_index()
   E  DOC
0  1   14
1  2    9
2  3    8
3  4    8
>>> 


2022-09-20 12:34

If you have any answers or tips


© 2022 pinfo. All rights reserved.