Answers for "append 2 dataframes"

0

append two dataframes pandas

import pandas as pd

# First DataFrame
df1 = pd.DataFrame({'id': ['A01', 'A02', 'A03', 'A04'],
                    'Name': ['ABC', 'PQR', 'DEF', 'GHI']})
  
# Second DataFrame
df2 = pd.DataFrame({'id': ['B05', 'B06', 'B07', 'B08'],
                    'Name': ['XYZ', 'TUV', 'MNO', 'JKL']})
  
frames = [df1, df2]
  
result = pd.concat(frames)
Posted by: Guest on July-16-2021
3

Joins with another DataFrame

# Joins with another DataFrame

df.join(df2, df.name == df2.name, 'outer').select(
  df.name, df2.height).collect()
# [Row(name=None, height=80), Row(name=u'Bob', height=85), Row(
#   name=u'Alice', height=None)]

df.join(df2, 'name', 'outer').select('name', 'height').collect()
# [Row(name=u'Tom', height=80), Row(name=u'Bob', height=85), Row(
#   name=u'Alice', height=None)]

cond = [df.name == df3.name, df.age == df3.age]
df.join(df3, cond, 'outer').select(df.name, df3.age).collect()
# [Row(name=u'Alice', age=2), Row(name=u'Bob', age=5)]

df.join(df2, 'name').select(df.name, df2.height).collect()
# Row(name=u'Bob', height=85)]

df.join(df4, ['name', 'age']).select(df.name, df.age).collect()
# [Row(name=u'Bob', age=5)]
Posted by: Guest on April-20-2020
0

python dataframe left join

>>> left.merge(right, on='user_id', how='left')
Posted by: Guest on April-10-2020

Python Answers by Framework

Browse Popular Code Answers by Language