【发布时间】:2018-10-22 17:33:08
【问题描述】:
在这里,我尝试使用 for 循环将数据帧 A 和 B 与 C 连接起来。
data = [['Alex',10],['Bob',12],['Clarke',13]]
A = pd.Dataframe(data, columns=['Name','Age'])
B = pd.Dataframe(data, columns=['Name','Age'])
C = pd.Dataframe(data, columns=['Name','Age'])
A.columns ---> Index(['Name', 'Age'], dtype='object')
B.columns ---> Index(['Name', 'Age'], dtype='object')
C.columns ---> Index(['Name', 'Age'], dtype='object')
for df in [A, B]:
df = pd.concat([df, C], axis=1)
A.columns ---> Index(['Name', 'Age'], dtype='object')
B.columns ---> Index(['Name', 'Age'], dtype='object')
df.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')
为什么不将 C 与原始 A、B 数据帧连接起来。为什么要创建一个新的 df Dataframe?
我想要在 for 循环之后:
A.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')
B.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')
【问题讨论】:
标签: python pandas for-loop concat