Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

combine 2 dataframes based on equal values in columns

import pandas as pd
pd.merge(df1, df2, on="Name")

#Only rows are kept for which common keys are found in both data frames. 
#In case you want to keep all rows from the left data frame and only add values from df2 
#where a matching key is available, you can use how="left":

pd.merge(df1, df2, on="Name", how="left")
Comment

pandas column name equal to another column value

In [2]: df
Out[2]:
    A  B
0  p1  1
1  p1  2
2  p3  3
3  p2  4

In [3]: df.loc[df['B'] == 3, 'A']
Out[3]:
2    p3
Name: A, dtype: object

In [4]: df.loc[df['B'] == 3, 'A'].iloc[0]
Out[4]: 'p3'
Comment

pandas set one column equal to another

In [300]:
df.loc[df['colA'] == 'a', 'colC'] = df['colB']
df['colC'] = df['colC'].fillna(0)
df

Out[300]:
   id colA  colB  colC
0   0    a    13    13
1   1    a    52    52
2   2    b    16     0
3   3    a    34    34
4   4    b   946     0
Comment

PREVIOUS NEXT
Code Example
Python :: python cron job virtualenv 
Python :: torch cos 
Python :: pandas get tuples from dataframe 
Python :: string in python 
Python :: Uninstalling/removing a package is very easy with pip: 
Python :: string remove suffix python 
Python :: python lambda function use global variable 
Python :: python loops 
Python :: are tuples in python mutable 
Python :: run multiprocesses on flask 
Python :: collections.defaultdict(set) 
Python :: conda create new env 
Python :: how to draw dendrogram in python 
Python :: standard deviation in python without numpy 
Python :: windows python absolute path 
Python :: reshape IML matrix 
Python :: mac big sur and python3 problems 
Python :: cv2 videowriter python not working 
Python :: python skip input 
Python :: chat application in python 
Python :: load list from file python 
Python :: how to handle response from tkinter messagebox.askquestion() function in Python 
Python :: current date to midnight 
Python :: xlabel not showing matplotlib 
Python :: django venv activate 
Python :: os.filename 
Python :: pandas df tail 
Python :: escape brackets in regex python 
Python :: pandas filter rows by column value regex 
Python :: pivot table but keep nan 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =