简体   繁体   中英

Find values in a Pandas dataframe and insert the data in a column of another Pandas dataframe

I have a dataframe that I need to convert the Custom Field column rows to columns in a second dataframe. This part I have managed to do and it works fine.

The problem is that I need to add the corresponding values from the id column to the respective columns of the second dataframe.

Here is an example:

This is first dataframe:

在此处输入图像描述

This is the second dataframe, with the columns already converted.

在此处输入图像描述

But I would like to add the values corresponding to the id column of the first dataframe to the second dataframe:

在此处输入图像描述

Attached is the code:

import pandas as pd
Data = {
"Custom Field": ["CF1", "CF2", "CF3"],
"id": [50, 40, 45],
"Name": ["Wilson", "Junior", "Otavio"]
}

### create the dataframe ###
df = pd.DataFrame(data)
print(df)


### add new columns from a list ###
columns_list  = []
for x in df['Custom Field']:
### create multiple columns with x ##
  columns_list.append(x)  
### convert list to new columns ###
df2 = pd.DataFrame(df,columns=columns_list)
df2["Name"] = df["Name"]
print(df2)

### If Name df3 is equal to Name df and equal to Custom Field of df, then get the id of df   and insert the value into the corresponding column in df3. ###
#### First unsuccessful attempt ###
df2_columns_names = list(df2.columns.values)
for df2_name in df2['Name']:
  for df2_cf in df2_columns_names:
    for df_name in df['Name']:
      for df_cf in df['Custom Field']:
        for df_id in df['id']:
          if df2_name == df_name and df2_cf == df_cf:
            df2.loc[df2_name, df2_cf] = df_id
 print(df2)  

Any suggestions?

Thanks in advance.

Use pivot_table

df.pivot_table(index=['Name'], columns=['Custom Field'])

As a general rule of thumb, if you are doing for loops and changing cells manually, you're using pandas wrong. Explore the methods of the framework in the docs, it can be very powerful :)

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