【问题标题】:Concatenate files with different and overlapping columns连接具有不同和重叠列的文件
【发布时间】:2016-07-01 10:40:03
【问题描述】:

我有三个 csv 文件。

一个有标题的:

names1=['Date','Conc','Flow','SZ','SB','RZ','RB','Fraction','Attenuation','Conc_less_-200_flag','Conc_greater_500_flag','Wind Speed','Wind Direction','Wind_direction_Flag','Wind_Speed_Less_than_4','Middle','Wind_Speed_Greater_than_10','Multiple conditions']

第一行:

Date,Conc,Flow,SZ,SB,RZ,RB,Fraction,Attenuation,Conc_less_-200_flag,Conc_greater_500_flag,Wind Speed,Wind Direction,Wind_direction_Flag,Wind_Speed_Less_than_4,Middle,Wind_Speed_Greater_than_10,Multiple conditions
2004-02-27 00:00:00,,,,,,,,,,,6.524999999999999,177.75,0.0,0.0,1.0,0.0,0.0
2004-02-27 01:00:00,,,,,,,,,,,6.991666666666667,197.83333333333334,0.0,0.0,1.0,0.0,0.0

另外两个:

names2=['Date','Chanel0','Chanel1','Chanel2','Chanel3','Chanel4','Chanel5','Chanel6','Chanel7','Conc_less_-200_flag','Conc_greater_500_flag','Wind Speed','Wind Direction','Wind_direction_Flag','Wind_Speed_Less_than_4','Middle','Wind_Speed_Greater_than_10','Multiple conditions']

第一行:

Date,Chanel0,Chanel1,Chanel2,Chanel3,Chanel4,Chanel5,Chanel6,Chanel7,Conc_less_-200_flag,Conc_greater_500_flag,Wind Speed,Wind Direction,Wind_direction_Flag,Wind_Speed_Less_than_4,Middle,Wind_Speed_Greater_than_10,Multiple conditions
2012-01-23 08:00:00,-2402.3575757575754,-2418.8121212121237,-2423.983863636366,-2422.913745454546,-2423.983863636366,-2422.814151515151,-2423.242424242424,-2422.4842121212123,1.0,1.0,,,,,,,
2012-01-23 09:00:00,6.5666666666666655,6.8849999999999945,0.02130000000000001,1.4343266666666665,0.02130000000000001,1.5671516666666663,1.0,2.085166666666667,1.0,1.0,,,,,,,

我希望输出是带有标题的 csv 文件:

['Date','Conc','Flow','SZ','SB','RZ','RB','Fraction','Attenuation','Chanel0','Chanel1','Chanel2','Chanel3','Chanel4','Chanel5','Chanel6','Chanel7','Conc_greater_500_flag','Wind Speed','Wind Direction','Wind_direction_Flag','Wind_Speed_Less_than_4','Middle','Wind_Speed_Greater_than_10','Multiple conditions']

很明显:后两个文件的贡献部分将有空白(或更好的 Nan's/0's for flow、sz 列等。第一个文件将在 channel0-7 列中包含它们

注意日期是索引列。

我尝试了df_merged=pd.concat(df1,df2,df3),但这似乎与标题重叠。

也试过了:

df_merged=pd.concat([df1,df2,df3],axis=1)

但这会导致 cs 输出: 进入

,Conc,Flow,SZ,SB,RZ,RB,Fraction,Attenuation,Conc_less_-200_flag,Conc_greater_500_flag,Wind Speed,Wind Direction,Wind_direction_Flag,Wind_Speed_Less_than_4,Middle,Wind_Speed_Greater_than_10,Multiple conditions,Chanel0,Chanel1,Chanel2,Chanel3,Chanel4,Chanel5,Chanel6,Chanel7,Conc_less_-200_flag,Conc_greater_500_flag,Wind Speed,Wind Direction,Wind_direction_Flag,Wind_Speed_Less_than_4,Middle,Wind_Speed_Greater_than_10,Multiple conditions,Chanel0,Chanel1,Chanel2,Chanel3,Chanel4,Chanel5,Chanel6,Chanel7,Conc_less_-200_flag,Conc_greater_500_flag,Wind Speed,Wind Direction,Wind_direction_Flag,Wind_Speed_Less_than_4,Middle,Wind_Speed_Greater_than_10,Multiple conditions
2004-02-27 00:00:00,,,,,,,,,,,6.524999999999999,177.75,0.0,0.0,1.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

很接近,但最后有多余的列 而且我不认为与常见的colomns重叠

【问题讨论】:

  • 试试df = pd.merge(df1,df2,df3); df.to_csv('New.csv')
  • df = pd.merge(df1,df2,df3) 给出:ValueError:DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

标签: python csv concatenation


【解决方案1】:

这是想法。 先将两个文件读入列表,然后设置并合并两个列表

你可以查看这个答案How to get the union of two lists using list comprehension?

【讨论】:

  • 我就是用这个来加载它们的进入列表?该答案适用于标题...但是尝试时会丢失所有数据
猜你喜欢
  • 2022-10-14
  • 2020-09-04
  • 2019-06-04
  • 2011-12-04
  • 2012-09-26
  • 2021-01-23
  • 2020-10-21
  • 1970-01-01
  • 2015-06-07
相关资源
最近更新 更多