简体   繁体   English

将 csv 文件列组合在一起 Pandas Python

[英]Combining csv files columns together Pandas Python

I am trying to combine file1-3.csv so that I could get the expected result.我正在尝试组合file1-3.csv以便获得预期的结果。 I want to combine all the rows together on all 3 file, but disregard the 1st column as it is the same on all 3 files.我想将所有 3 个文件上的所有行组合在一起,但忽略第一列,因为它在所有 3 个文件上都是相同的。 How can i do this with pandas.我怎么能用 pandas 做到这一点。

Code:代码:

import pandas as pd 

file1 = pd.read_csv('STDOutputs_Q1.csv')
file2 = pd.read_csv('STDOutputs_Q2.csv')
file3 = pd.read_csv('STDOutputs_Q3.csv')

Inside file1.csv内部文件1.csv

element,LNPT,SNPT
[ 2.  2. 30.],89,60
[ 2.  2. 40.],999,77

Inside file2.csv里面文件2.csv

element,MxU,MxD,TT
[ 2.  2. 30.],17127,-3,0
[ 2.  2. 40.],17141,-40,2

Inside file3.csv里面文件3.csv

element,TNT
[ 2.  2. 30.],1000
[ 2.  2. 40.],30

Expected Results:预期成绩:

element,LNPT,SNPT,MxU,MxD,TT,TNT
[ 2.  2. 30.],89,60,17127,-3,0,1000
[ 2.  2. 40.],999,77,17141,-40,2,30

You can use pd.join like:您可以使用pd.join像:

q1_2 = file1.join(file2, lsuffix='_Q1', rsuffix='_Q2')
file1-3 = q1_2.join(file3,  rsuffix='_Q3')

Or if the 'element' column is the same for all three data frame, and there are no conflicting column names, you can use pd.merge :或者,如果所有三个数据框的“元素”列都相同,并且没有冲突的列名,则可以使用pd.merge

q1_2 = file1.merge(file2)
file1-3 = q1_2.merge(file3)

You can use Umar'sconcat() idea, but need to add axis=1 :您可以使用 Umar 的concat()想法,但需要添加axis=1

pd.concat([d.set_index('element') for d in [file1,file2,file3]], axis=1)

# element        LNPT  SNPT    MxU  MxD  TT   TNT
# [ 2.  2. 30.]    89    60  17127   -3   0  1000
# [ 2.  2. 40.]   999    77  17141  -40   2    30

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

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