【问题标题】:Concatenate columns with different lengths based on specific columns in Pandas根据 Pandas 中的特定列连接不同长度的列
【发布时间】:2021-02-05 07:28:56
【问题描述】:

我有两个不同的 txt 文件,包含相同数量但长度不同的列,即,

file1.txt

1650,A,2428057182945480,0.33446294,0.102967925,-0.3460815
1650,A,2428057232445480,0.086256325,0.719756,0.45393208
1650,A,2428057281945480,-0.04051014,1.1011207,1.0462191
1650,B,2428301534869292,-1.6426647,0.8912665,-4.4452224
1650,B,2428301584369292,-1.6128372,1.1938016,-3.1242943
1650,B,2428301633869292,-3.6656017,1.328025,-1.8204107
1650,B,2428301683369292,-6.0336843,2.2516093,-1.7117537
1650,B,2428301732869292,-3.2778456,-0.43924874,-1.3911091

file2.txt

1650,A,2428057133445480,-1.2798505,-5.187936,-2.3116016
1650,A,2428057182945480,-3.3029509,-6.8231754,-4.011485
1650,A,2428057232445480,-2.876783,-8.365042,-7.171831
1650,A,2428057281945480,-2.2542906,-8.5661545,-8.153454
1650,A,2428057331445480,-3.2646437,-10.953174,-8.826224
1650,B,2428301485369292,6.3887777,-0.42347443,0.82480246
1650,B,2428301534869292,8.522012,-16.99614,9.446322

可以看出,两个文件在 A 和 B 中的长度不同。我想使用 pandas 将它们连接起来,结果如下:

1650,A,2428057133445480,NaN,NaN,NaN,-1.2798505,-5.187936,-2.3116016
1650,A,2428057182945480,0.33446294,0.102967925,-0.3460815,-3.3029509,-6.8231754,-4.011485
1650,A,2428057232445480,0.086256325,0.719756,0.45393208,-2.876783,-8.365042,-7.171831
1650,A,2428057281945480,-0.04051014,1.1011207,1.0462191,-2.2542906,-8.5661545,-8.153454
1650,A,2428057331445480,NaN,NaN,NaN,-3.2646437,-10.953174,-8.826224
1650,B,2428301485369292,NaN,NaN,NaN,6.3887777,-0.42347443,0.82480246
1650,B,2428301534869292,8.522012,-16.99614,9.446322,-1.6426647,0.8912665,-4.4452224
1650,B,2428301584369292,-1.6128372,1.1938016,-3.1242943,NaN,NaN,NaN
1650,B,2428301633869292,-3.6656017,1.328025,-1.8204107,NaN,NaN,NaN
1650,B,2428301683369292,-6.0336843,2.2516093,-1.7117537,NaN,NaN,NaN
1650,B,2428301732869292,-3.2778456,-0.43924874,-1.3911091,NaN,NaN,NaN

根据我的理解,我可以先生成dataframes,然后像这样使用concatenate

df1 = read_data('file1.txt')
df2 = read_data('file2.txt')
pd.concat([df1,df2], ignore_index=True, axis=1)

这是正确的吗?如果不是,如何解决这个问题?

另外,如何删除带有Nan的行,使得结果变为

1650,A,2428057182945480,0.33446294,0.102967925,-0.3460815,-3.3029509,-6.8231754,-4.011485
1650,A,2428057232445480,0.086256325,0.719756,0.45393208,-2.876783,-8.365042,-7.171831
1650,A,2428057281945480,-0.04051014,1.1011207,1.0462191,-2.2542906,-8.5661545,-8.153454
1650,B,2428301534869292,8.522012,-16.99614,9.446322,-1.6426647,0.8912665,-4.4452224

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    我不知道列名,所以我只是输入虚拟列名:

    df1 = pd.read_csv('untitled.txt') # this is the first txt with columns abcdef
    df2 = pd.read_csv('untitled1.txt') # this is the second txt with columns abcghi
     
    df1.merge(df2, how='outer', on=['a','b','c']).dropna() # this gives what you want
    
    

    【讨论】:

    • df1 的 ['a','b','c'] 值被 df2 替换。如何解决这个问题?
    • a,b 和 c 是前 3 列,它们是两个文本文件中的列。您正在使用它们作为连接的键。它们在两个文本文件中都是相同的,以使行存在。
    • 我明白了。我将 a、b、c 误解为最后 3 列。
    猜你喜欢
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 2012-07-03
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多