简体   繁体   English

追加额外列的熊猫数据框

[英]Pandas Dataframe Appending Extra Column

I have a pandas.DataFrame that have the following columns: ['agent', 'agentid', 'agentname', 'agentaddress'] .我有一个 pandas.DataFrame 有以下列: ['agent', 'agentid', 'agentname', 'agentaddress']

When I concat this data frame with another data frame of same cardinality, it appends an extra column at the end with same name.当我将此数据框与另一个具有相同基数的数据框连接时,它会在末尾附加一个具有相同名称的额外列。

>>> print(df1)
   agent  agentid agentname agentaddress
0      1     1001      test        delhi
>>> print(df2)
   agent  agentid agentname agentaddress
0      2     1002     test1     vadodara

When I concat these two data frames, it gives me the following result:当我连接这两个数据框时,它给了我以下结果:

>>> print(pd.concat([df1, df2], sort=False))
   agent  agentid agentname agentaddress   agentid
0      1   1001.0      test        delhi       NaN
0      2      NaN     test1     vadodara    1002.0

我创建了单独的 csv 文件并尝试了你在这里所做的:它工作得很好,检查列的名称是否在两个文件中都是正确的。

Try to check the dtypes of entire colums in both dataframes.尝试检查两个数据帧中整个列的 dtypes。 Sometimes ids can be of categorical dtype有时 ids 可以是分类 dtype

The column names 'agentid' is different in df1 and df2, so you get this result. df1 和 df2 中的列名称“agentid”不同,因此您会得到此结果。 Please recheck the column names 'agentid', ensure they are the same character string.请重新检查列名“agentid”,确保它们是相同的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM