KEGG Drug database Python script

Question:

I have a drug database saved in a SINGLE column in CSV file that I can read with Pandas. The file containts 750000 rows and its elements are devided by "///". The column also ends with "///". Seems every row is ended with ";".

I would like to split it to multiple columns in order to create structured database. Capitalized words (drug information) like "ENTRY", "NAME" etc. will be headers of these new columns.

So it has some structure, although the elements can be described by different number and sort of information. Meaning some elements will just have NaN in some cells. I have never worked with such SQL-like format, it is difficult to reproduce it as Pandas code, too. Please, see the PrtScs for more information.

An example of desired output would look like this:

df = pd.DataFrame({
      "ENTRY":["001", "002", "003"],
      "NAME":["water", "ibuprofen", "paralen"],
      "FORMULA":["H2O","C5H16O85", "C14H24O8"],
      "COMPONENT":[NaN, NaN, "paracetamol"]})

I am guessing there will be .split() involved based on CAPITALIZED words? The Python 3 code solution would be appreciated. It can help a lot of people. Thanks!

TXT prstc
Pandas prtsc
Excel prtsc

Asked By: Mr.Slow

||

Answers:

Whatever he could, he helped:

import pandas as pd

cols = ['ENTRY', 'NAME', 'FORMULA', 'COMPONENT']
# We create an additional dataframe.
dfi = pd.DataFrame()
# We read the file, get two columns and leave only the necessary lines.
df = pd.read_fwf(r'drug', header=None, names=['Key', 'Value'])
df = df[df['Key'].isin(cols)]
# To "flip" the dataframe, we first prepare an additional column
# with indexing by groups from one 'ENTRY' row to another.
dfi['Key1'] = dfi['Key'] = df[(df['Key'] == 'ENTRY')].index
dfi = dfi.set_index('Key1')
df = df.join(dfi, lsuffix='_caller', rsuffix='_other')
df.fillna(method="ffill", inplace=True)
df = df.astype({"Key_other": "Int64"})
# Change the shape of the table.
df = df.pivot(index='Key_other', columns='Key_caller', values='Value')
df = df.reindex(columns=cols)
# We clean up the resulting dataframe a little.
df['ENTRY'] = df['ENTRY'].str.split(r's+', expand=True)[0]
df.reset_index(drop=True, inplace=True)
pd.set_option('display.max_columns', 10)

Small code refactoring:

import pandas as pd

cols = ['ENTRY', 'NAME', 'FORMULA', 'COMPONENT']

# We read the file, get two columns and leave only the necessary lines.
df = pd.read_fwf(r'C:Usersфdrugdrug', header=None, names=['Key', 'Value'])
df = df[df['Key'].isin(cols)]

# To "flip" the dataframe, we first prepare an additional column
# with indexing by groups from one 'ENTRY' row to another.
df['Key_other'] = None
df.loc[(df['Key'] == 'ENTRY'), 'Key_other'] = df[(df['Key'] == 'ENTRY')].index
df['Key_other'].fillna(method="ffill", inplace=True)

# Change the shape of the table.
df = df.pivot(index='Key_other', columns='Key', values='Value')
df = df.reindex(columns=cols)

# We clean up the resulting dataframe a little.
df['ENTRY'] = df['ENTRY'].str.split(r's+', expand=True)[0]
df['NAME'] = df['NAME'].str.split(r'(', expand=True)[0]
df.reset_index(drop=True, inplace=True)
pd.set_option('display.max_columns', 10)
print(df)

Key     ENTRY                                        NAME             FORMULA  
0      D00001                                      Water                  H2O   
1      D00002                                     Nadide        C21H28N7O14P2   
2      D00003                                     Oxygen                   O2   
3      D00004                             Carbon dioxide                  CO2   
4      D00005                Flavin adenine dinucleotide        C27H33N9O15P2   
...       ...                                         ...                 ...   
11983  D12452  Fostroxacitabine bralpamide hydrochloride   C22H30BrN4O8P. HCl   
11984  D12453                                Guretolimod         C24H34F3N5O4   
11985  D12454                               Icenticaftor         C12H13F6N3O3   
11986  D12455                             Lirafugratinib          C28H24FN7O2   
11987  D12456               Lirafugratinib hydrochloride     C28H24FN7O2. HCl   

Key   COMPONENT  
0           NaN  
1           NaN  
2           NaN  
3           NaN  
4           NaN  
...         ...  
11983       NaN  
11984       NaN  
11985       NaN  
11986       NaN  
11987       NaN  

[11988 rows x 4 columns]

Need a little more to bring to mind, I leave it to your work.

Answered By: Сергей Кох
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.