分享

pandas

 新用户31171414 2020-06-12

I would use list to keep all DataFrames

all_df = [] # list
all_df.append(df_1)
all_df.append(df_2)

and then I would no need exec

for rows in df_list.itertuples():
    row = rows.Index
    ind_id = df_list.loc[row]['Prodinfo']
    print(row, ind_id)
    all_df[row]["rank"] = all_df[row][ind_id].rank(ascending=True)

Eventually I would use dictionary

all_df = {} # dict
all_df['PRODUCT.AREA'] = df_1
all_df['PRODUCT.CODE'] = df_2

and then I don't need exec and df_list

for key, df in all_df.items():
    df["rank"] = df[key].rank(ascending=True)

Minimal working code with list

import pandas as pd

all_df = [] # list

datum = {
    'Products': ['Stocks', 'Bonds', 'Notes'],
    'PRODUCT.AREA': ['10200', '50291', '50988']
}
all_df.append( pd.DataFrame(datum) )

datum = {
    'Products': ['Stocks', 'Bonds', 'Notes'],
    'PRODUCT.CODE': ['66', '55', '22']
}
all_df.append( pd.DataFrame(datum) )

#print( all_df[0] )
#print( all_df[1] )

print('--- before ---')
for df in all_df:
    print(df)

summary = {'Prodinfo': ['PRODUCT.AREA', 'PRODUCT.CODE']}
df_list = pd.DataFrame(summary, columns=['Prodinfo'])
#print(df_list)

for rows in df_list.itertuples():
    row = rows.Index
    ind_id = df_list.loc[row]['Prodinfo']
    #print(row, ind_id)
    all_df[row]["rank"] = all_df[row][ind_id].rank(ascending=True)

print('--- after ---')
for df in all_df:
    print(df)

Minimal working code with dict

import pandas as pd

all_df = {} # dict

datum = {
    'Products': ['Stocks', 'Bonds', 'Notes'],
    'PRODUCT.AREA': ['10200', '50291', '50988']
}
all_df['PRODUCT.AREA'] = pd.DataFrame(datum)


datum = {
    'Products': ['Stocks', 'Bonds', 'Notes'],
    'PRODUCT.CODE': ['66', '55', '22']
}
all_df['PRODUCT.CODE'] = pd.DataFrame (datum)

print('--- before ---')
for df in all_df.values():
    print(df)

for key, df in all_df.items():
    df["rank"] = df[key].rank(ascending=True)

print('--- after ---')
for df in all_df.values():
    print(df)

Frankly, for two dataframes I wouldn't waste time for df_list and for-loop

import pandas as pd

datum = {
    'Products': ['Stocks', 'Bonds', 'Notes'],
    'PRODUCT.AREA': ['10200', '50291', '50988']
}
df_0 = pd.DataFrame(datum)

datum = {
    'Products': ['Stocks', 'Bonds', 'Notes'],
    'PRODUCT.CODE': ['66', '55', '22']
}
df_1 = pd.DataFrame(datum)

print('--- before ---')
print( df_0 )
print( df_1 )

df_0["rank"] = df_0['PRODUCT.AREA'].rank(ascending=True)    
df_1["rank"] = df_1['PRODUCT.CODE'].rank(ascending=True)    

print('--- after ---')
print( df_0 )
print( df_1 )

And probably even I would put all in one dataframe

import pandas as pd

df = pd.DataFrame({
    'Products': ['Stocks', 'Bonds', 'Notes'],
    'PRODUCT.AREA': ['10200', '50291', '50988'],
    'PRODUCT.CODE': ['66', '55', '22'],
})

print('--- before ---')
print( df )

#df["rank PRODUCT.AREA"] = df['PRODUCT.AREA'].rank(ascending=True)    
#df["rank PRODUCT.CODE"] = df['PRODUCT.CODE'].rank(ascending=True)    

for name in ['PRODUCT.AREA', 'PRODUCT.CODE']:
    df[f"rank {name}"] = df[name].rank(ascending=True)    

print('--- after ---')
print( df )

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多