让我说我有以下df:
df = pd.DataFrame({'$a':[1,2], '$b': [10,20],'$A':[1,2], '$B': [10,20]})
,我需要用一个变量将标头连接起来 可以说我有:
headers = ['NY', 'TA']
我想做类似的事情
for h in headers:
df.rename(columns=lambda x: x.rename(h + x), inplace=True)
我知道x.rename(h + x)
是错误的。请建议如何获得:
NY $A NY $B NY $a NY $b TA $A TA $B TA $a TA $b
0 1 10 1 10 1 10 1 10
1 2 20 2 20 2 20 2 20
这是完整的循环
from datetime import datetime
import pandas_datareader.data as web
import pandas as pd
start = datetime(2010, 1, 1)
end = pd.datetime.now()
datasets_original_test = ['AAPL', 'MSFT']
for d in datasets_original_test:
data_original = web.DataReader(d, 'yahoo', start, end)
#THIS IS MY PROBLEM##########################
data_original.rename(columns=lambda x: x.concate(d, x), inplace=True)
#THIS IS MY PROBLEM##########################
datasets_test.append(data_original)
df_returns = pd.concat(datasets_test, axis=1, join_axes=[datasets_test[0].index])
答案 0 :(得分:3)
这是一种动态的方式:
df_out = pd.concat([df]*len(headers), axis=1, keys=headers)
或使用@ Wen-Ben的快捷方式:
df_out = pd.concat(dict.fromkeys(headers,df),1)
df_out.columns = df_out.columns.map(' '.join)
df_out
输出:
NY $a NY $b NY $A NY $B TA $a TA $b TA $A TA $B
0 1 10 1 10 1 10 1 10
1 2 20 2 20 2 20 2 20
答案 1 :(得分:2)
您可以使用add_prefix
pd.concat([df.add_prefix(h) for h in headers], axis = 1, sort = False)
NY$a NY$b NY$A NY$B TA$a TA$b TA$A TA$B
0 1 10 1 10 1 10 1 10
1 2 20 2 20 2 20 2 20
如果需要空间,请使用
pd.concat([df.add_prefix(h + ' ') for h in headers], axis = 1, sort = False)