pandas map one column to the combination of two columns
Use map
df['List'] = df['List'].map(df.set_index('Numb')['Name'])
List Numb Name
0 one 1 one
1 one 2 two
2 two 3 three
3 four 4 four
4 three 5 five
How to map a function using multiple columns in pandas?
Use pd.DataFrame.apply()
, as below:
df['d'] = df.apply(lambda x: some_func(a = x['a'], b = x['b'], c = x['c']), axis=1)
NOTE: As @ashishsingal asked about columns, the axis
argument should be provided with a value of 1, as the default is 0 (as in the documentation and copied below).
axis : {0 or ‘index’, 1 or ‘columns’}, default 0
- 0 or ‘index’: apply function to each column
- or ‘columns’: apply function to each row
Mapping multiple columns to a single dataframe with pandas
You could use df2[col].map(df1['Salary'])
to map each column of df2
according to df1['Salary']
:
import pandas as pd
df1 = pd.DataFrame({'Salary':[8700,6300,4700,2100,3400]}, index=pd.Series(['Joe Smith', 'Jane Doe', 'Rob Dole', 'Sue Pam', 'Jack Li'], name='Name'))
df2 = pd.DataFrame({'Captain':['Sue Pam', 'Jane Doe', 'Rob Dole', 'Joe Smith', 'Rob Dole'], 'Skipper': ['Joe Smith', 'Sue Pam', 'Joe Smith', 'Sue Pam', 'Jack Li']})
df3 = pd.DataFrame({col: df2[col].map(df1['Salary']) for col in df2})
print(df3)
yields
Captain Skipper
0 2100 8700
1 6300 2100
2 4700 8700
3 8700 2100
4 4700 3400
Assign unique ID to combination of two columns in pandas dataframe independently on their order
Try np.sort
:
a = np.sort(df, axis=1)
df['id'] = df.groupby([a[:,0],a[:,1]]).ngroup() + 1
Output:
col1 col2 id
0 1 2 1
1 2 1 1
2 2 3 2
3 3 2 2
4 3 4 3
5 4 3 3
Pandas: map column using a dictionary on multiple columns
two steps,
first lets turn those None values to NaNs
so we can use numeric operations.
df['category'] = pd.to_numeric(df['category']) # add arg ,errors='coerce' if needed.
2nd, lets use groupby transform and max to fill only the NaN values.
df["category"] = df["category"].fillna(
df.groupby(["company", "product"])["category"].transform("max")
)
print(df)
company product category
0 Company1 Product A 1.0
1 Company1 Product A 2.0
2 Company1 Product F 3.0
3 Company1 Product A 2.0
4 Company2 Product F 5.0
5 Company2 Product F 5.0
Pandas map according to values in multiple columns
You can use merge:
df2.merge(df1, how="left")
See also the merging section of the docs.
Combination of two column names from a given pandas dataframe
Use:
from itertools import combinations
print (list(combinations(df.columns, 2)))
[('name', 'degree'), ('name', 'score'), ('degree', 'score')]
output_list = [list(x) for x in combinations(df.columns, 2)]
print(output_list)
[['name', 'degree'], ['name', 'score'], ['degree', 'score']]
output_list = list(map(list, combinations(df.columns, 2)))
print(output_list)
[['name', 'degree'], ['name', 'score'], ['degree', 'score']]
Mapping a new column in a pandas dataframe based on columns in another dataframe which share a column
You could use Series.map:
df['new_val'] = df.ID.map(df_map.set_index('ID').squeeze())
Output
ID val new_val
0 0 5 10
1 1 10 20
2 2 20 30
3 0 10 10
4 1 0 20
5 2 15 30
Notice that squeeze in df_map.set_index('ID').squeeze()
converts the DataFrame into a Series.
Related Topics
Plotting Data from Multiple Pandas Data Frames in One Plot
Retrieving Subfolders Names in S3 Bucket from Boto3
How to Make a Discord Bot Leave a Server from a Command in Another Server
Grab a Number After a String in a File
Calculate Monthly Returns from Daily Returns in Pandas(Cumpound)
Python Executable Not Finding Libpython Shared Library
Finding the Value of the Min and Max Pixel
Remove Last Few Characters in Pyspark Dataframe Column
Python: How to Read and Load an Excel File from Aws S3
Remove Very First Row in Pandas
How to Clear All Widgets from a Tkinter Window in One Go Without Referencing Them All Directly
How to Remove a Single Quotes from a List
How to Read a List of Parquet Files from S3 as a Pandas Dataframe Using Pyarrow
Pandas Dataframe Check If Column Value Exists in a Group of Columns
Text Pre-Processing + Python + Csv:Removing Special Characters from a Column of a Csv