简体   繁体   English

将熊猫中来自不同df的不同列合并为一个df时出错

[英]Error in concating different columns from different dfs into one df in pandas

I have two CSV files(attached screenshots). 我有两个CSV文件(随附屏幕截图)。 One with Datetime column and one without Datetime column. 一种带有日期时间列,另一种没有日期时间列。 I want to join these both dfs into one df(attached screenshot). 我想将这两个df合并为一个df(随附的屏幕截图)。 So I tried 所以我尝试了

main_data = pd.concat([Group_avg, weather_avg], axis=1, join='inner')
main_data.columns = ['Avg Current(mA)', 'Avg T-in(degC)', 'T-out(degC)', 'RH-out(%)']

And when I printed main_data, there were only column names present without any data(as shown in screenshot). 当我打印main_data时,只有列名存在而没有任何数据(如屏幕截图所示)。

Desired output 所需的输出

CSV 1 CSV 1

CSV 2 Error CSV 2 错误

Assuming rows in each file correspond to each other you can merge on the index: 假设每个文件中的行彼此对应,则可以在索引上合并:

main_data = pd.merge(Group_avg, weather_avg, left_index=True, right_index=True)

You can also specify different merge types (eg left, right, innner, outer). 您还可以指定不同的合并类型(例如,左,右,内部,外部)。 See here for documentation. 有关文档,请参见此处

If the indexes don't correspond in the files then you will need a way of matching up the rows from each file when merging as currently it doesn't look like there's any obvious column to merge on in both files. 如果索引在文件中不对应,那么您将需要一种在合并时匹配每个文件中的行的方法,因为目前看来这两个文件中似乎没有任何明显的列可以合并。

Also, when reading in the CSV files, remove the index_col = 0 parameter, as this is telling pandas to use the first column in each file as the row index, which doesn't look right. 另外,在读取CSV文件时,请删除index_col = 0参数,因为这告诉熊猫将每​​个文件中的第一列用作行索引,但看起来不太正确。

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

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