How to insert a row in a DataFrame Pandas in the middle of other rows?

1

I have a sensor data output that has the following structure:

--- Home ---

$ LAGM, Colar03, Yellow, 32262, -31226, -5120, -104,40,190,1662.00,1670.00,236.00, MGAL $ GPGGA, 113203,181,206,6812, S, 05443,4264, W, 1,03,3,4,0,0, M, 4.8, M , 0000 * 68 $ GPGSA, A, 2.07,23,30 , 3,5,3,4,0,9 * 3E 0.15 0.05 $ GPGSV, 3,3,12,02,10,263,24,05,05,218,28,16,00,146, 27,00,118, * 70 $ GPRMC, 113203181, A, 2026.6812, S, 05443.4264, W, 000.0.000.0,241017 , A * 63 $ GPVTG, 000.0, T , M, 000.0, N, 000.0, K, A * 0D

--- End ---

Where information in the Pandas DataFrame is positioned sequentially, such as a table and its records. It turns out that the sensor, in some blocks of code, simply did not register some data, leaving with missing information, according to the following block:

--- Home ---

$ LAGM, Colar03, Yellow, 6.27904,6144,332, -172, -216,1536,109.24, MGAL $ GPGGA, 120025.0, N, O, E, 0.0.0.0, M, O, MŽÆF|F & $ GPRMC, 120025, V, 0, N, 0, E, 0.0.280606 , N * 78 $ GPVTG, 0, T , M, 0, N, 0, K, N * 02

--- End ---

You may notice missing information as well as the existence of non-ASCII characters (this is another treatment). My main contribution in the Master is preprocessing, where it consists of retrieving these missing lines and placing a mean between the values, rescuing the lost information.

However, in excel it is possible to execute this "in the arm" according to the animated gif below:

Where it drags the existing records down so we can add new data to the created lines, but I could not find a way to add this information to Pandas just like the image.

I would like to know, is it possible to perform this action by inserting a line with new information in Pandas with some function or even bypassing it with Python?

    
asked by anonymous 31.07.2018 / 18:28

1 answer

1

Assuming we have this DataFrame:

d = {'nome': ['maria', 'Pedro', 'Mario'], 'idade': [30, 45, 36], 'estado': ['SP', 'BA', 'RJ']}
df = pd.DataFrame(data=d)

>>> print df
  estado  idade   nome
0     SP     30  maria
1     BA     45  Pedro
2     RJ     36  Mario

Using this function I copied from here: link

def inserir_linha(idx, df, df_inserir):
    dfA = df.iloc[:idx, ]
    dfB = df.iloc[idx:, ]

    df = dfA.append(df_inserir).append(dfB).reset_index(drop = True)

    return df

This function breaks the DataFrame in two (dfA and dfB) into a predetermined id (idx) and then joins dfA with df_addressed with dfB in that order.

Using the function:

d_iserido = d = {'nome': ['nome_iserido1','nome_2'], 'idade': [0,100], 'estado': ['EXEMPLO', 'olaa']}
df_iserido = pd.DataFrame(data = d_iserido)
df = inserir_linha(1, df, df_iserido)

>>> print df
    estado  idade           nome
0       SP     30          maria
1  EXEMPLO      0  nome_iserido1
2     olaa    100         nome_2
3       BA     45          Pedro
4       RJ     36          Mario
    
31.07.2018 / 19:41