Skip to content

Instantly share code, notes, and snippets.

@lethee
Created February 25, 2021 09:50
Show Gist options
  • Save lethee/4d301cb69485ea085945172f250824ee to your computer and use it in GitHub Desktop.
Save lethee/4d301cb69485ea085945172f250824ee to your computer and use it in GitHub Desktop.
dataframe from_dict, join, concat, update
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
# In[69]:
d = {
'alice': {'c1': '11', 'c2': '12'},
'bob': {'c1': '21', 'c2': '22'},
'chan': {'c1': '31', 'c2': '32'},
}
d2 = {
'dave': {'c1': 'a1', 'c2': 'a2'},
'elvis': {'c1': 'b1', 'c2': 'b2'},
'bob': {'c3': 'B2', 'c4': 'B2'},
}
# In[70]:
df1 = pd.DataFrame.from_dict(d, orient='index')
df1
# In[71]:
df2 = pd.DataFrame.from_dict(d2, orient='index')
df2
# In[72]:
new_df = pd.concat([df1, df2])
new_df
# In[73]:
new_df = df1.join(df2, rsuffix='r')
new_df
# In[74]:
new_df = df1.join(df2, how='inner', rsuffix='r')
new_df
# In[79]:
df1.update(df2)
df1
# In[80]:
df1 = pd.DataFrame.from_dict(d, orient='index', columns=['c1', 'c2', 'c3', 'c4'])
df1.update(df2)
df1
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment