2017-09-07 2 views
-1

Ich habe diesen Datenrahmen:Split und Fliesen ein Pandas Datenrahmen

df: 

id . city . state . person1 . P1phone1 . P1phone2 . person2 . P2phone1 . P2phone2 
1 . Ghut . TY . Jeff . 32131 . 4324  . Bill . 213123 . 31231 
2 . Ffhs . TY . Ron  . 32131 . 4324  . Bill . 213123 . 31231 
3 . Tyuf . TY . Jeff . 32131 . 4324  . Tyler . 213123 . 31231 

Ich will es so aussehen:

df: 
id . city . state . person . phone1 . phone2 
1 . Ghut . TY . Jeff . 32131 . 4324 
2 . Ghut . TY . Bill . 213123 . 31231 
3 . Ffhs . TY . Ron  . 32131 . 4324 
4 . Ffhs . TY . Bill . 213123 . 31231 
5 . Tyuf . TY . Jeff . 32131 . 4324 
6 . Tyuf . TY . Tyler . 213123 . 31231 

Ich habe eine ziemlich harte Zeit, das zu tun versuchen. Kann jemand helfen?

+0

I Reihen ernst wird nicht empfohlen hartzucodieren. –

Antwort

1

können Sie schneiden nur die Datenrahmen in zwei:

In [20]: df1 = df[['city','state','person1','P1phone1','P1phone2']] 

In [21]: df2 = df[['city','state','person2','P2phone1','P2phone2']] 

und dann stellen Sie sicher, dass sie die gleichen Spalten haben:

In [27]: df1.columns = ['city','state','person','phone1','phone2'] 

In [28]: df2.columns = ['city','state','person','phone1','phone2'] 

Und dann eines auf das andere anhängen:

In [29]: df1.append(df2) 
Out[29]: 
    city state person phone1 phone2 
0 Ghut TY Jeff 32131 4324 
1 Ffhs TY Ron 32131 4324 
2 Tyuf TY Jeff 32131 4324 
0 Ghut TY Bill 213123 31231 
1 Ffhs TY Bill 213123 31231 
2 Tyuf TY Tyler 213123 31231 
1

versuchen Sie diesen Bruder

pd.concat(df.loc[:,['id','city','state','person1','P1phone1','P1phone2']]. 
rename(columns = {'person1' : 'person', 'P1phone1' : 'phone1', 'P1phone2' : 'phone2'), 
    df.loc[:,['id','city','state','person2','P2phone1','P2phone2']]. 
rename(columns = {'person1' : 'person', 'P2phone1' : 'phone1', 'P2phone2' : 'phone2'), axis = 0) 
3
df 

id city state person1 P1phone1 P1phone2 person2 P2phone1 P2phone2 
1 Ghut TY  Jeff  32131  4324  Bill  213123 31231 
2 Ffhs TY  Ron  32131  4324  Bill  213123 31231 
3 Tyuf TY  Jeff  32131  4324  Tyler 213123 31231 

df = df.set_index(['city', 'state']) 
df.columns = np.tile(df.columns[:3].values, 2) 

Mit df.append

df = df.iloc[:, :3].append(df.iloc[:, 3:]).reset_index() 
df 

    city state person1 P1phone1 P1phone2 
0 Ghut TY Jeff  32131  4324 
1 Ffhs TY  Ron  32131  4324 
2 Tyuf TY Jeff  32131  4324 
3 Ghut TY Bill 213123  31231 
4 Ffhs TY Bill 213123  31231 
5 Tyuf TY Tyler 213123  31231 

pd.concat

df = pd.concat([df.iloc[:, :3].append(df.iloc[:, 3:])]).reset_index() 
df 

    city state person1 P1phone1 P1phone2 
0 Ghut TY Jeff  32131  4324 
1 Ffhs TY  Ron  32131  4324 
2 Tyuf TY Jeff  32131  4324 
3 Ghut TY Bill 213123  31231 
4 Ffhs TY Bill 213123  31231 
5 Tyuf TY Tyler 213123  31231 
+0

yeah, yeah, mag ich das –

+0

@ ℕʘʘḆḽḘ Yeah ... das Spielen mit Spaltennamen kann ermüdend schnell werden ;-) –

+0

Ich denke nur, es ist pd.wide_to_long Frage :) – Wen

1

Verwendung Dies ist eine typisch pd.wide_to_long Frage

Versuchen Sie, diese

df=df.rename(columns={'P1phone1':'phone1P1','P1phone2':'phone2P1','P2phone1':'phone1P2','P2phone2':'phone2P2'}) 

    pd.wide_to_long(df,['person','phone1P','phone2P'],i=['id','city','state'],j='age').reset_index().drop('age',1) 
Out[364]: 
    id city state person phone1P phone2P 
0 1 Ghut TY Jeff 32131  4324 
1 1 Ghut TY Bill 213123 31231 
2 2 Ffhs TY Ron 32131  4324 
3 2 Ffhs TY Bill 213123 31231 
4 3 Tyuf TY Jeff 32131  4324 
5 3 Tyuf TY Tyler 213123 31231 
0

ich mit diesem zu langsam war, aber hier ist meine Lösung:

import pandas as pd 
import numpy as np 

#define column names for the result df. 
# another way to ensure the same column names: colnames = df.columns.values[:6] 
colnames = ['identity', 'city', 'state', 'person', 'phone1', 'phone2'] 

#split the original df into two. 
df1 = df.iloc[:, :6] 
df2 = pd.concat([df.iloc[:, :3], df.iloc[:, 6:]], axis=1) 

#reset the column names so that both dfs have same colnames 
df1.columns, df2.columns = colnames, colnames 

#concatenate the two 
result = pd.concat([df1, df2]).reset_index(drop=True) 

identity city state person phone1 phone2 
0   1 Ghut TY Jeff 32131 4324 
1   2 Ffhs TY Ron  32131 4324 
2   3 Tyuf TY Jeff 32131 4324 
3   1 Ghut TY Bill 213123 31231 
4   2 Ffhs TY Bill 213123 31231 
5   3 Tyuf TY Tyler 213123 31231 
Verwandte Themen