简体   繁体   English

如何使用不同的dtypes连接两个数据帧?

[英]How can I join two dataframes with different dtypes?

I have a dataframe(df1) with index as a date range and no columns specified and another dataframe(df2) with float values in every column. 我有一个数据框(df1),索引作为日期范围,没有指定列,另一个数据框(df2)在每列中都有浮点值。 I tried joining a specific column from df2 to df1 using .join() method and ended up with all values as NaN in df1. 我尝试使用.join()方法将特定列从df2连接到df1,最后在df1中将所有值作为NaN。 What should I do to solve this? 我该怎么做才能解决这个问题?

It's unclear what you mean without any example of the data or their shape, and without more details about what kind of 'join' you're trying to do. 如果没有任何数据或形状的例子,并且没有关于你正在尝试做什么样的“加入”的更多细节,目前还不清楚你的意思。 It sounds like you are trying to concatenate dataframes without relying on a column or index level names to join on. 听起来您正在尝试连接数据帧而不依赖于要加入的列或索引级别名称。 That's what join or merge try to do, so if you don't have common values on the on parameter of the join , you'll end up with nan s. 这就是joinmerge尝试这样做,所以如果你没有共同的价值观对on的参数join ,你会最终nan秒。 If I'm correct and you just want a concatenation of dataframes, then you can use concat . 如果我是正确的,你只想要连接数据帧,那么你可以使用concat I can't provide the code without more details, but it would look something like this: 我不能提供没有更多细节的代码,但它看起来像这样:

new_df = pd.concat([df1, df2[['whatever_column_you_want_to_concatenate']]], axis=1)

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

相关问题 如何将两个数据框连接在一起 - How can I join two dataframes together 如何加入 pandas 中具有不同行数和不同列的两个数据帧? - How can I join two dataframes in pandas that have different no of rows and different columns? 如何通过 Pyspark 中的不同字段连接两个数据帧 - How to join two dataframes by different fields in Pyspark 当我最初不知道dtypes时,如何快速更改100多个数据帧的格式? - How can I change the format of over 100 dataframes quickly when I don't initially know the dtypes? awswrangler 返回具有不同数据类型的数据帧 - awswrangler returning dataframes with different dtypes 如何合并两个具有不同索引的数据帧? - how can i merge two dataframes with different indices? 如何“行绑定”具有不同列的两个熊猫数据框? - How can I 'row bind' two pandas dataframes with different columns? 如何在熊猫中将两个具有不同列标签的数据框相乘? - How can I multiply two dataframes with different column labels in pandas? 如何加入两个数据框,其中一列包含两个或多个值 - How can I join two dataframes where one column holds two or more values 如何在pydatatable中加入具有不同键列名称的两个数据框? - How to join two dataframes with different key column names in pydatatable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM