我想通过向第一个 (df
) 添加一列来连接两个相同长度的数据帧。
但由于某些 df
行正在被过滤,因此索引似乎不匹配。
import pandas as pd
pd.read_csv(io.StringIO(uploaded['customer.csv'].decode('utf-8')), sep=";")
df["Margin"] = df["Sales"]-df["Cost"]
df = df.loc[df["Margin"]>-100000]
df = df.loc[df["Sales"]> 1000]
df.reindex()
df
返回:
所以这个操作:
customerCluster = pd.concat([df, clusters], axis = 1, ignore_index= True)
print(customerCluster)
回来了:
所以,我已经尝试过 reindex 和参数 ignore_index = True,正如你在上面看到的,但我的 Python 技能很差..
答案 0 :(得分:0)
谢谢大家的回答。如果有人遇到同样的问题,我找到的解决方案是这样的:
customerID = df["CustomerID"]
customerID = customerID.reset_index(drop=True)
df = df.reset_index(drop=True)
所以,基本上,两个数据帧的索引现在是匹配的,因此:
customerCluster = pd.concat((customerID, clusters), axis = 1)
这将正确连接两个数据框。