合并csv文件并添加新行

当我尝试合并我的csv文件时,每个csv文件的最后两列被放置在同一列中。我该如何解决这个问题?

第二个问题,我想为每个文件添加一行作为输出。

这是我的代码和示例;

import globimport pandas as pdimport ospath = r'/Users/isozyesil/PycharmProjects/P300/Data/TestP300'  # use your pathallFiles = glob.glob(path + "/*.csv")frame = pd.DataFrame()list_ = []for file_ in allFiles:    df = pd.read_csv(file_, index_col=None, header=0)    list_.append(df)frame = pd.concat(list_)print(list_)

这是我问题的示例;

Last 6 row of A.csv : 4  -1.6144  -0.7137  1.2791  -0.82055  -2.4333  -0.9433  0.9616  -2.33926  -2.3548   0.0945  0.9933  -3.11747  -3.0944  -0.8559  0.6441  -2.89768  -0.4791  -0.8795  0.7027  -2.7338 9  -3.6540  -6.2700  0.2650  -4.0410Last First 4 row of B.csv :  0  -3.1024  -1.8710  0.1311  -5.70711  -0.1855  -0.8241 -0.2756  -6.69622  -3.2941  -2.0557  0.2471  -4.49693  -1.5004  -0.2482  0.9565  -0.6866Here is what happens when I run my code : 4  -1.6144  -0.7137  1.2791  -0.82055  -2.4333  -0.9433  0.9616  -2.33926  -2.3548   0.0945  0.9933  -3.11747  -3.0944  -0.8559  0.6441  -2.89768  -0.4791  -0.8795  0.7027  -2.7338,  -3.6540  -6.2700  0.2650 -4.0410,0  -3.1024  -1.8710  0.1311  -5.70711  -0.1855  -0.8241 -0.2756  -6.69622  -3.2941  -2.0557  0.2471  -4.49693  -1.5004  -0.2482  0.9565  -0.6866Here is what I dream to do4  -1.6144  -0.7137  1.2791  -0.8205  A5  -2.4333  -0.9433  0.9616  -2.3392  A6  -2.3548   0.0945  0.9933  -3.1174  A7  -3.0944  -0.8559  0.6441  -2.8976  A8  -0.4791  -0.8795  0.7027  -2.7338  A9  -3.6540  -6.2700  0.2650  -4.0410  A0  -3.1024  -1.8710  0.1311  -5.7071  B1  -0.1855  -0.8241 -0.2756  -6.6962  B2  -3.2941  -2.0557  0.2471  -4.4969  B3  -1.5004  -0.2482  0.9565  -0.6866  B

提前感谢每一位Python战士。


回答:

import osimport globimport pandas as pdfiles = glob.glob(r'D:\temp\.data\43076965\*.csv')x = pd.concat(    [pd.read_csv(f, header=None, delim_whitespace=True)       .assign(f=os.path.basename(os.path.splitext(f)[0]))     for f in files],    ignore_index=True)x.to_csv(r'd:/temp/out.csv', header=None, index=False, sep='\t')

结果(d:/temp/out.csv):

4   -1.6144 -0.7137 1.2791  -0.8205 A5   -2.4333 -0.9433 0.9616  -2.3392 A6   -2.3548 0.0945  0.9933  -3.1174 A7   -3.0944 -0.8559 0.6441  -2.8976 A8   -0.4791 -0.8795 0.7027  -2.7338 A9   -3.654  -6.27   0.265   -4.041  A0   -3.1024 -1.871  0.1311  -5.7071 B1   -0.1855 -0.8241 -0.2756 -6.6962 B2   -3.2941 -2.0557 0.2471  -4.4969 B3   -1.5004 -0.2482 0.9565  -0.6866 B

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注