当我尝试连接我的csv文件时,每个csv文件的最后两列放在同一列中。怎么能解决这个问题?
第二个问题,我想将新行添加为每个文件的输出。
这是我的代码和示例;
import glob
import pandas as pd
import os
path = r'/Users/isozyesil/PycharmProjects/P300/Data/TestP300' # use your path
allFiles = 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.8205
5 -2.4333 -0.9433 0.9616 -2.3392
6 -2.3548 0.0945 0.9933 -3.1174
7 -3.0944 -0.8559 0.6441 -2.8976
8 -0.4791 -0.8795 0.7027 -2.7338
9 -3.6540 -6.2700 0.2650 -4.0410
Last First 4 row of B.csv :
0 -3.1024 -1.8710 0.1311 -5.7071
1 -0.1855 -0.8241 -0.2756 -6.6962
2 -3.2941 -2.0557 0.2471 -4.4969
3 -1.5004 -0.2482 0.9565 -0.6866
Here is what happens when I run my code :
4 -1.6144 -0.7137 1.2791 -0.8205
5 -2.4333 -0.9433 0.9616 -2.3392
6 -2.3548 0.0945 0.9933 -3.1174
7 -3.0944 -0.8559 0.6441 -2.8976
8 -0.4791 -0.8795 0.7027 -2.7338, -3.6540 -6.2700 0.2650 -4.0410,
0 -3.1024 -1.8710 0.1311 -5.7071
1 -0.1855 -0.8241 -0.2756 -6.6962
2 -3.2941 -2.0557 0.2471 -4.4969
3 -1.5004 -0.2482 0.9565 -0.6866
Here is what I dream to do
4 -1.6144 -0.7137 1.2791 -0.8205 A
5 -2.4333 -0.9433 0.9616 -2.3392 A
6 -2.3548 0.0945 0.9933 -3.1174 A
7 -3.0944 -0.8559 0.6441 -2.8976 A
8 -0.4791 -0.8795 0.7027 -2.7338 A
9 -3.6540 -6.2700 0.2650 -4.0410 A
0 -3.1024 -1.8710 0.1311 -5.7071 B
1 -0.1855 -0.8241 -0.2756 -6.6962 B
2 -3.2941 -2.0557 0.2471 -4.4969 B
3 -1.5004 -0.2482 0.9565 -0.6866 B
提前感谢每个蟒蛇战士。
答案 0 :(得分:1)
import os
import glob
import pandas as pd
files = 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 A
5 -2.4333 -0.9433 0.9616 -2.3392 A
6 -2.3548 0.0945 0.9933 -3.1174 A
7 -3.0944 -0.8559 0.6441 -2.8976 A
8 -0.4791 -0.8795 0.7027 -2.7338 A
9 -3.654 -6.27 0.265 -4.041 A
0 -3.1024 -1.871 0.1311 -5.7071 B
1 -0.1855 -0.8241 -0.2756 -6.6962 B
2 -3.2941 -2.0557 0.2471 -4.4969 B
3 -1.5004 -0.2482 0.9565 -0.6866 B