Merging column values in a data frame in Pandas / Python

Related searches

I'm trying to merge the values of columns (Columns B and C) within the same dataframe. B and C sometimes have the same values. Some values in B are present in C while some values in C are present in B. The final results would show one column that is the combination of the two columns.

Initial data:
 A          B          C            D
Apple    Canada        ''          RED
Bananas    ''          Germany     BLUE
Carrot     US          US          GREEN
Dorito     ''          ''          INDIGO

Expected Data:
 A          B         C
Apple    Canada      RED
Bananas  Germany      BLUE
Carrot     US        GREEN
Dorito     ''        INDIGO

You can sort strings and take the last one:

df['B'] = df[['B', 'C']].apply(lambda x: x.sort_values()[1], axis=1)

df=df.drop('C', 1).rename(columns={'D':'C'})    
print(df)

Output:

         A        B       C
0    Apple   Canada     RED
1  Bananas  Germany    BLUE
2   Carrot       US   GREEN
3   Dorito       ''  INDIGO

pandas.DataFrame.combine — pandas 1.1.1 documentation, The value to fill NaNs with prior to passing any column to the merge func. overwritebool Combine two DataFrame objects and default to non-null values in frame calling the method. Example using a true element-wise combine function. You can use pd.merge: import pandas as pd pd.merge(df1, df2, on="movie_title") 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".

IIUC

df['B']=df[['B','C']].replace("''",np.nan).bfill(1).loc[:,'B']
df=df.drop('C',1).rename(columns={'D':'C'})
df
Out[102]: 
         A        B       C
0    Apple   Canada     RED
1  Bananas  Germany    BLUE
2   Carrot       US   GREEN
3   Dorito      NaN  INDIGO

Merge, join, and concatenate — pandas 0.20.3 documentation, Note the index values on the other axes are still respected in the join. Without a little bit of context and example many of these arguments don't In the case of DataFrame, the indexes must be disjoint but the columns do not need to be:. I have a Python Pandas dataframe df: d=[['hello',1,'GOOD','long.kw'], [1.2,'chipotle',np.nan,'bingo'], ['various',np.nan,3000,123.456]] t=pd.

Another way would be to make smart use of list comprehension:

# Make sets of the column B and C combined to get rid of duplicates
k = [set(b.strip() for b in a) for a in zip(df['B'], df['C'])]

# Flatten sets to strings
k = [''.join(x) for x in k]

# Create desired column
df['B'] = k
df.drop('C', axis=1, inplace=True)

print(df)
         A        B       D
0    Apple   Canada     RED
1  Bananas  Germany    BLUE
2   Carrot       US   GREEN
3   Dorito           INDIGO

Combining DataFrames with Pandas – Data Analysis and , Data Analysis and Visualization in Python for Ecologists The pandas package provides various methods for combining DataFrames including merge and concat . The columns containing the common values are called “join key(s)”. Joining� Merge two data frames in Python on column with non-unique values [duplicate] pandas create new column based on values from other columns / apply a function of

Combining Data in Pandas With merge(), .join(), and , Combining Series and DataFrame objects in Pandas is a powerful rows in the merge column that repeat the same values (such as 1, 1, 3, 5,� In this short guide, I’ll show you how to concatenate column values in pandas DataFrame. To start, you may use this template to concatenate your column values (for strings only): df1 = df ['1st Column Name'] + df ['2nd Column Name'] + Notice that the plus symbol (‘+’) is used to perform the concatenation.

Merge and Join DataFrames with Pandas in Python, Where there are missing values of the “on” variable in the left� pandas.DataFrame.combine ¶ DataFrame.combine(other, func, fill_value=None, overwrite=True) [source] ¶ Perform column-wise combine with another DataFrame. Combines a DataFrame with other DataFrame using func to element-wise combine columns.

Python | Read csv using pandas.read_csv() Python | Merge, Join and Concatenate DataFrames using Panda; Python | Delete rows/columns from DataFrame using Pandas.drop() Python | Data Comparison and Selection in Pandas; Machine Learning and Artificial Intelligence; Difference between Machine learning and Artificial Intelligence

Comments
  • What if B and C had different values?