Is it possible to extract only the parts you want from the data frame read by Python csv?

Asked 6 days ago, Updated 6 days ago, 1 views

Hello, I'm a beginner at Python.

I am handling csv and Excel with DataFrame. I'd like to ask for your help because there's a blockage while trying this and that. As shown in the figure below, when there is a csv file, the part I want is below a2 and I want to pick the value above NaN (p, q in the csv file in the example). The problem here is that the information we know about the csv file is only a1, a2. It is located unconditionally at (1,A) of a1. a2 is located on the csv file (random, A). And p and q are also random alphabets, and not p and q, but b, c, d, e may exist in different rows. In other words, the data I want is under a2 and I want to separate all the data above the first NaN value under a2. What should I do in this case? I ask for your help meㅠ<


2022-09-20 15:31

1 Answers

df = pd.DataFrame(
        ['a1', 2, 3],
        ['b', 5, 6],

        # -------------
        ['c', 5, 6],
        ['d', 5, 6],
        ['a2', 5, 6],
        ['e', 5, 6],
        # -------------
        ['NaN', 5, 6],

        ['f', 5, 6],
        ['g', 5, 6],

        # -------------
        ['e', 5, 6],
        # -------------
        ['NaN', 5, 6],

        ['h', 8, 9],
        ['a3', 5, 6],
    columns=['A', 'B', 'C'])

a2_index_list = df.index[df['A'] == 'a2'].tolist()

for i in a2_index_list:
    print('-' * 20)
    tempDf = df[i:]
    nextNan_indexList = tempDf.index[tempDf['A'] == 'NaN'].tolist()
    if len(nextNan_indexList) > 0:

2022-09-20 15:31

If you have any answers or tips

© 2022 pinfo. All rights reserved.