- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在 Customer_ID
列上合并两个数据帧 df1, df2
。似乎 Customer_ID
在两者中具有相同的数据类型 (object
)。
df1:
Customer_ID | Flag
12345 A
df2:
Customer_ID | Transaction_Value
12345 258478
当我合并两个表时:
new_df = df2.merge(df1, on='Customer_ID', how='left')
对于某些 Customer_ID,它有效,而对于其他 Customer_ID,则无效。对于这个例子,我会得到这个结果:
Customer_ID | Transaction_Value | Flag
12345 258478 NaN
我检查了数据类型,它们是相同的:
df1.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 873353 entries, 0 to 873352
Data columns (total 2 columns):
Customer_ID 873353 non-null object
Flag 873353 non-null object
dtypes: object(2)
memory usage: 20.0+ MB
df2.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 873353 entries, 0 to 873352
Data columns (total 2 columns):
Customer_ID 873353 non-null object
Transaction_Value 873353 int64
dtypes: object(2)
memory usage: 20.0+ MB
当我上传 df1 时,我确实收到了这条消息:
C:\Users\xxx\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py:2717: DtypeWarning: Columns (1) have mixed types. Specify dtype option on import or set low_memory=False.
interactivity=interactivity, compiler=compiler, result=result)
当我想检查是否存在客户 ID 时,我意识到我必须在两个数据框中以不同方式指定它。
df1.loc[df1['Customer_ID'] == 12345]
df2.loc[df2['Customer_ID'] == '12345']
最佳答案
Customer_ID
在这两种情况下都是 dtype==object
......但这并不意味着各个元素是同一类型。您需要同时制作 str
或 int
使用 int
dtype = dict(Customer_ID=int)
df1.astype(dtype).merge(df2.astype(dtype), 'left')
Customer_ID Flag Transaction_Value
0 12345 A 258478
使用 str
dtype = dict(Customer_ID=str)
df1.astype(dtype).merge(df2.astype(dtype), 'left')
Customer_ID Flag Transaction_Value
0 12345 A 258478
关于python pandas column dtype = object 导致合并失败 : DtypeWarning: Columns have mixed types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44639772/
这个问题在这里已经有了答案: Pandas read_csv: low_memory and dtype options (13 个答案) 关闭去年。 我从数据框创建了一个 .csv 文件,如下所示
我正在使用以下内容读取 Pandas Dataframe: df = pd.read_csv('../table.csv.gz', nrows=10000, inde
我有这些列: ['Campaign', 'Ad group', 'Keyword', 'Status', 'Match type', 'Max. CPC', 'Quality score', 'Imp
当我在 pandas 中导入一个 csv 文件时,我得到一个 DtypeWarning: 第 (3) 列具有混合类型。在导入时指定 dtype 选项或设置 low_memory=False。 如何找出
我正在尝试读取 csv 文件 pd.set_option('display.max_columns', None) inventory = pd.read_csv('inventory-new.csv
我正在尝试在 Customer_ID 列上合并两个数据帧 df1, df2。似乎 Customer_ID 在两者中具有相同的数据类型 (object)。 df1: Customer_ID | Fla
我是一名优秀的程序员,十分优秀!