2016-06-27 13 views
0

Ich habe zwei pandasDataFrames. Sie haben die gleichen Spalten, und so möchte ich die Daten von jedem nehmen und es in eine größere DataFrame setzen. Das Problem ist, dass die Nummer, die jede Zeile identifiziert, wörtlich kopiert wird, während ich möchte, dass sie intelligent aktualisiert wird.Verketten Sie zwei Pandas DataFrames, ohne Zeilen-IDs zu duplizieren?

Hier ist, was ich habe:

import pandas as pd 

df_one = pd.DataFrame([ 
    {'animal': 'cat', 'color': 'black'}, 
    {'animal': 'dog', 'color': 'brown'}]) 
df_two = pd.DataFrame([ 
    {'animal': 'fish', 'color': 'red'}, 
    {'animal': 'bird', 'color': 'blue'}]) 

print(pd.DataFrame([df_one, df_two])) 

Welche Ausgänge:

animal color 
0 cat  black 
1 dog  brown 
0 fish red 
1 bird blue 

Der Ausgang ich will, ist:

animal color 
0 cat  black 
1 dog  brown 
2 fish red 
3 bird blue 

Antwort

3

Verwenden concat mit ignore_index=True:

>>> pandas.concat([df_one, df_two], ignore_index=True) 
    animal color 
0 cat black 
1 dog brown 
2 fish red 
3 bird blue 
1

Ich mag @ BrenBarns Antwort. Sie könnten abwechselnd tun:

>>> df_concat = pd.concat([df_one, df_two]).reset_index(drop=True) 
>>> df_concat 
    animal color 
0 cat black 
1 dog brown 
2 fish red 
3 bird blue 
Verwandte Themen