简体   繁体   中英

iterating through multiple columns and appending data in pandas dataframe

在此处输入图像描述

Input dataframe:

 dtf= {'A': ['00 12 3b 01 00 00 00 00','00 13 3b 01 00 00 00 00','00 14 3b 01 00 00 00 00'], 'B': ['59 d0 7f 10 27 f8 7f ff 7f ','60 d0 7f 10 27 f8 7f ff 7f ','61 d0 7f 10 27 f8 7f ff 7f ','62 d0 7f 10 27 f8 7f ff 7f ','64 d0 7f 10 27 f8 7f ff 7f ','65 d0 7f 10 27 f8 7f ff 7f ','66 d0 7f 10 27 f8 7f ff 7f ','67 d0 7f 10 27 f8 7f ff 7f ','69 d0 7f 10 27 f8 7f ff 7f ','70 d0 7f 10 27 f8 7f ff 7f ','71 d0 7f 10 27 f8 7f ff 7f ','72 d0 7f 10 27 f8 7f ff 7f ','73 d0 7f 10 27 f8 7f ff 7f ']}

I have a data frame with columns A&B. I want to append for the values in column B with the values in column A such that the output should look like this:

  • 59 d0 7f 10 27 f8 7f ff 7f 00 12 3b 01 00 00 00 00

    60 d0 7f 10 27 f8 7f ff 7f 00 12 3b 01 00 00 00 00

    61 d0 7f 10 27 f8 7f ff 7f 00 12 3b 01 00 00 00 00

    62 d0 7f 10 27 f8 7f ff 7f 00 12 3b 01 00 00 00 00

  • 64 d0 7f 10 27 f8 7f ff 7f 00 13 3b 01 00 00 00 00

    65 d0 7f 10 27 f8 7f ff 7f 00 13 3b 01 00 00 00 00

    66 d0 7f 10 27 f8 7f ff 7f 00 13 3b 01 00 00 00 00

    67 d0 7f 10 27 f8 7f ff 7f 00 13 3b 01 00 00 00 00

  • 69 d0 7f 10 27 f8 7f ff 7f 00 14 3b 01 00 00 00 00

    70 d0 7f 10 27 f8 7f ff 7f 00 14 3b 01 00 00 00 00

    71 d0 7f 10 27 f8 7f ff 7f 00 14 3b 01 00 00 00 00

    72 d0 7f 10 27 f8 7f ff 7f 00 14 3b 01 00 00 00 00

    73 d0 7f 10 27 f8 7f ff 7f 00 14 3b 01 00 00 00 00

    This is a random sample but the iterative loop should work for th entire data. I'm new to python and couldn't figure out any way(s) to do it. The values in the dataframe are of hex str type. I'm expeccting the output in the form of a list.

I am assuming that your dataframe looks exactly as it does in the screenshot.

You can forward fill the null values in column A and then concatenate the values from both columns. Since column B seems to be empty when A has a value, you can get rid of these rows to get the result in the bullet list.

# Fill null values forward
df['A'] = df['A'].ffill()

# Concatenate `B` and `A`
df['new_col'] = df['B'].astype(str) + df['A'].astype(str)

# Get rid of rows where `B` is empty
df = df[df['B'].notna()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM