Skip to content Skip to sidebar Skip to footer

Sorting And Arranging A List Using Pandas

I have an input file as shown below which needs to be arranged in such an order that the key values need to be in ascending order, while the keys which are not present need to be p

Solution 1:

Replace your ds line with

ds = [{int(pair[0]): pair[1] for pair in [w.split('=', 1) for w in x]} for x in s]

To convert the index to an integer so it will be sorted numerically

To output the n/a values at the end, you could use the pandas selection to output the nonnull values first, then the null values, e.g:

for (ix, series) in p.iterrows():
    print('\nindex[%d]' % ix)
    output_series(ix, series[pd.notnull])
    output_series(ix, series[pd.isnull].fillna('n/a'))

btw, you can also simplify your stack, groupby, print to:

for (ix, series) in p1.iterrows():
    print('\nindex[%d]' % ix)
    for tag, value in series.iteritems():
        print(tag, '\t', value)

So the whole script becomes:

defoutput_series(ix, series):
    for tag, value in series.iteritems():
        print(tag, '\t', value)

df = pd.read_csv('inputfile', index_col=None, names=['text'])
s = df.text.str.split('|')
ds = [{int(pair[0]): pair[1] for pair in [w.split('=', 1) for w in x]} for x in s]
p = pd.DataFrame.from_records(ds)
for (ix, series) in p.iterrows():
    print('\nindex[%d]' % ix)
    output_series(ix, series[pd.notnull])
    output_series(ix, series[pd.isnull].fillna('n/a'))

Solution 2:

Here:

import pandas as pd
import numpy as np

df = pd.read_csv('inputfile', index_col=None, names=['text'])
s = df.text.str.split('|')
ds = [dict(w.split('=', 1) for w in x) for x in s]
p1 = pd.DataFrame.from_records(ds).fillna('n/a')
st = p1.stack(level=0,dropna=False)
for k, v in st.groupby(level=0):
    print(k, v.sort_index())

Post a Comment for "Sorting And Arranging A List Using Pandas"