I'm still stuck on a transformation issue.
My goals is to convert rows included into a dataframe in a new dataframe
df0 = {'Table':['BKPF','BKPF','BKPF','BSEG','BSEG'],
'Zone':['TCODE', 'BLDAT', 'BLART','HKONT','GSBER'],
'Type':['CHAR','DATE','CHAR','CHAR','CHAR'],
'Len' :[10,8,2,10,4]}
into 2 new dataframes as
I'm new in Python, I have tried by different process like:
df0 = pd.read_csv(Ztable, sep=';')
df1 = pd.DataFrame(df0)
t_bkpf = df1.loc[df1['Table'] == 'BBKPF']
t_bseg = df1.loc[df1['Table'] == 'BBSEG']
or with
t_bkpf = pd.DataFrame()
t_bseg = pd.DataFrame()
for index, row in df1.iterrows():
if row['Table'] == 'BKPF':
t_bkpf[row['Zone']] = np.nan
if row['Table'] == 'BSEG':
t_bseg[row['Zone']]= np.nan
df_bkpf = t_bkpf.copy()
df_bseg = t_bseg.copy()
The result based on code above it the best I have done but I have warning message on performance, I would like avoid it.
On next topic, I need to move data from one other Dataframe to these Dataframe based on columns defined.
Thanks
CodePudding user response:
You can do below:
df1 = pd.DataFrame(df0)
t = df1.set_index('Table')
dfs = [t.loc[idx].pivot(columns='Zone').stack().T for idx in t.index.unique()]
First dataframe:
print(dfs[0]):
Table BKPF
Zone BLART BLDAT TCODE
Type CHAR DATE CHAR
Len 2 8 10
Second dataframe:
print(dfs[1]):
Table BSEG
Zone GSBER HKONT
Type CHAR CHAR
Len 4 10
CodePudding user response:
df = pd.DataFrame.from_dict(df0_dict, orient='index')
df
index | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
Table | BKPF | BKPF | BKPF | BSEG | BSEG |
Zone | TCODE | BLDAT | BLART | HKONT | GSBER |
Type | CHAR | DATE | CHAR | CHAR | CHAR |
Len | 10 | 8 | 2 | 10 | 4 |
rows = ['Zone', 'Type', 'Len']
df_BKPF = df.loc[rows, df.loc['Table'] == 'BKPF']
df_BKPF
index | 0 | 1 | 2 |
---|---|---|---|
Zone | TCODE | BLDAT | BLART |
Type | CHAR | DATE | CHAR |
Len | 10 | 8 | 2 |
df_BSEG = df.loc[rows, df.loc['Table'] == 'BSEG']
df_BSEG
index | 3 | 4 |
---|---|---|
Zone | HKONT | GSBER |
Type | CHAR | CHAR |
Len | 10 | 4 |