【发布时间】:2021-04-28 04:33:35
【问题描述】:
我正在尝试在 Pandas 中合并大约 101 个 CSV 文件。每个文件都有 2 个时间列和一个“值”列。我想保留 2 次列,因为它们在 CSV 文件中是相同的,然后将 101 个 CSV 中的每一个的“值”列合并到一个新的 CSV 文件中。
使用 pd.merge 我可以使用以下合并 2 个文件
data1 = {'time': ['00:00','01:00','02:00'],
'local_time': ['09:30','10:30','11:30'],
'value': ['265.591','330.766','360.962']}
data2 = {'time': ['00:00','01:00','02:00'],
'local_time': ['09:30','10:30','11:30'],
'value': ['521.217','588.034','588.034']}
df_1 = pd.DataFrame(data1)
df_2 = pd.DataFrame(data2)
locs = ['_A11','_B10']
df_test = pd.merge(df_1,df_2, on=['time','local_time'], how='inner', suffixes = (locs)
)
print(df_test)
这会产生:
time local_time value_A11 value_B10
0 00:00 09:30 265.591 521.217
1 01:00 10:30 330.766 588.034
2 02:00 11:30 360.962 588.034
但是,我不太确定如何组合接下来的 99 个 csv 文件,或者这是否是完成这项任务的最佳方式。
我的目标是:
time local_time value_A11 value_B10 value_B11 ...
0 00:00 09:30 265.591 521.217 123 ...
1 01:00 10:30 330.766 588.034 456 ...
2 02:00 11:30 360.962 588.034 789 ...
任何帮助将不胜感激!
编辑 1:
Colin 的示例有效,但是我一直在将数据帧加载到这样的数组中:
import glob
import os
# create and sort list of file names in folder
fl = glob.glob('*.csv')
sorted_fl = sorted(fl)
# open csv files from list and store in df
df_list = [pd.read_csv(f, header=3) for f in sorted_fl]
#test df
df_list[0]
我想知道如何修改 for 循环以便它可以为数组提供数据?再次感谢!
编辑 2:从答案到编辑 1 的错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-144-772c1d15f228> in <module>
14 # loop through each dataframe and merge it with existing one
15 for i, df in enumerate(df_list[1:]):
---> 16 df_output = pd.merge(df_list[0], df, on=['time','local_time'], how='inner', suffixes = (['_' + str(i), '_' + str(i+1)]))
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
79 copy=copy,
80 indicator=indicator,
---> 81 validate=validate,
82 )
83 return op.get_result()
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate)
628 # validate the merge keys dtypes. We may need to coerce
629 # to avoid incompat dtypes
--> 630 self._maybe_coerce_merge_keys()
631
632 # If argument passed to validate,
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in _maybe_coerce_merge_keys(self)
1136 inferred_right in string_types and inferred_left not in string_types
1137 ):
-> 1138 raise ValueError(msg)
1139
1140 # datetimelikes must match exactly
ValueError: You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat
编辑 3
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-cce982321079> in <module>
11
12 # change datatype to datetime for first df
---> 13 df['local_time'] = pd.to_datetime(df_list[0]['local_time'])
14 df['time'] = pd.to_datetime(df_list[0]['time'])
15
NameError: name 'df' is not defined
【问题讨论】:
标签: python pandas csv merge concatenation