Combinando datos: concatenación y anexión
In [1]:
import pandas as pd
import numpy as np
In [2]:
def make_df(cols, ind):
"""Quickly make a DataFrame"""
data = {c: [str(c) + str(i) for i in ind]
for c in cols}
return pd.DataFrame(data, ind)
# example DataFrame
make_df('ABC', range(3))
Out[2]:
In [3]:
class display(object):
"""Display HTML representation of multiple objects"""
template = """<div style="float: left; padding: 10px;">
<p style='font-family:"Courier New", Courier, monospace'>{0}</p>{1}
</div>"""
def __init__(self, *args):
self.args = args
def _repr_html_(self):
return '\n'.join(self.template.format(a, eval(a)._repr_html_())
for a in self.args)
def __repr__(self):
return '\n\n'.join(a + '\n' + repr(eval(a))
for a in self.args)
Recordatorio: Concatenación de NumPy Arrays¶
In [4]:
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
np.concatenate([x, y, z])
Out[4]:
In [5]:
x = [[1, 2],
[3, 4]]
np.concatenate([x, x], axis=1)
Out[5]:
Concatenación simple con pd.concat
¶
In [6]:
ser1 = pd.Series(['A', 'B', 'C'], index=[1, 2, 3])
ser2 = pd.Series(['D', 'E', 'F'], index=[4, 5, 6])
pd.concat([ser1, ser2])
Out[6]:
In [7]:
df1 = make_df('AB', [1, 2])
df2 = make_df('AB', [3, 4])
display('df1', 'df2', 'pd.concat([df1, df2])')
Out[7]:
In [8]:
df3 = make_df('AB', [0, 1])
df4 = make_df('CD', [0, 1])
display('df3', 'df4', "pd.concat([df3, df4], axis='col')")
Out[8]:
Indices duplicados¶
In [9]:
x = make_df('AB', [0, 1])
y = make_df('AB', [2, 3])
y.index = x.index # make duplicate indices!
display('x', 'y', 'pd.concat([x, y])')
Out[9]:
Detectando indices repetidos como un error¶
In [10]:
try:
pd.concat([x, y], verify_integrity=True)
except ValueError as e:
print("ValueError:", e)
Ignorando el índice¶
In [11]:
display('x', 'y', 'pd.concat([x, y], ignore_index=True)')
Out[11]:
MultiIndex keys¶
In [12]:
display('x', 'y', "pd.concat([x, y], keys=['x', 'y'])")
Out[12]:
Concatenación con uniones¶
In [13]:
df5 = make_df('ABC', [1, 2])
df6 = make_df('BCD', [3, 4])
display('df5', 'df6', 'pd.concat([df5, df6])')
Out[13]:
In [14]:
display('df5', 'df6',
"pd.concat([df5, df6], join='inner')")
Out[14]:
In [15]:
display('df5', 'df6',
"pd.concat([df5, df6], join_axes=[df5.columns])")
Out[15]:
El método append()
¶
In [16]:
display('df1', 'df2', 'df1.append(df2)')
Out[16]: