简体   繁体   English

尝试对2个pandas数据框进行排序,然后从一个复制到另一个

[英]Trying to sort 2 pandas dataframe and then copy from one to another

I have two csv files I want to manipulate and then combine into one file. 我有两个要处理的csv文件,然后合并为一个文件。 I first converted them to pandas. 我首先将它们转换为熊猫。 One pandas dataframe looks like this: 一个熊猫数据框如下所示:

      Number  Quiz
0  111111145     0
1  111111108     1
2  111111123     1
3  111111114     0
4  111111132     0

the other like this: 另一个像这样:

Last Name First Name       Number   Quiz
0  Student1      Student1  111111123   
1  Student2      Student2  111111114   
2  Student3      Student3  111111132   
3  Student4      Student4  111111145   
4  Student5      Student5  111111108   

I want to end up with something like this: 我想结束这样的事情:

Last Name First Name       Number       Quiz
    0  Student1      Student1  111111108  1   
    1  Student2      Student2  111111114  0 
    2  Student3      Student3  111111123  1
    3  Student4      Student4  111111132  0 
    4  Student5      Student5  111111145  0

but when I run my code I end up getting: 但是当我运行代码时,我最终得到:

Last Name First Name       Number       Quiz
    0  Student1      Student1  111111108  0   
    1  Student2      Student2  111111114  1 
    2  Student3      Student3  111111123  0
    3  Student4      Student4  111111132  1 
    4  Student5      Student5  111111145  0

And I am not sure why. 我不确定为什么。 My code is as follows: 我的代码如下:

import argparse
import sys, re
import numpy as np
import smtplib
from random import randint
import csv
import math
import pandas as pd

parser = argparse.ArgumentParser()
parser.add_argument('-cname', '--c', help = 'column name to copy')
parser.add_argument('-source', '--s', help = 'source file with the column to copy')
parser.add_argument('-target', '--t', help = 'the target file with the names and UINS')
parser.add_argument('-out', '--f', help = 'output file with column copied')

if len(sys.argv)==1:
    parser.print_help()
    sys.exit(1)
args = parser.parse_args()



sourceFile = pd.read_csv(args.s)
targetFile = pd.read_csv(args.t)
print sourceFile
print targetFile
del targetFile[args.c]
sourceFile.sort_values('UIN', ascending = True, inplace = True)
targetFile.sort_values('UIN', ascending = True, inplace = True)
print sourceFile
print targetFile
targetFile[args.c]= sourceFile[args.c]
targetFile.to_csv(args.f, index = False)
print targetFile

you should use a merge to get your output : 您应该使用合并来获取输出:

merged = df1.merge(df2, on="Number")

Should work, but you might have a problem of duplicated "Quiz" column if it appears in df1. 应该可以,但是如果df1中出现“ Quiz”列重复的问题。

you can use the following to remove this problem (removes the quiz column from your first dataframe before it computes: 您可以使用以下方法消除此问题(在计算前从第一个数据帧中删除测验列:

merged = df1[df1.columns[:-1]].merge(df2, on="Number")

I changed it just a little and got it to work. 我做了一点修改就可以了。 I used 我用了

result = pd.merge(targetFile, sourceFile, on = 'number')

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

相关问题 根据 Pandas 中的 id 将列值从一个数据帧复制到另一个数据帧 - Copy column value from one dataframe to another based on id in Pandas 使用 Pandas 将列从一个 DataFrame 复制到另一个 DataFrame 的最快方法? - Fastest way to copy columns from one DataFrame to another using pandas? 使用 pandas dataframe 上的名称将图像从一个文件夹复制到另一个文件夹 - Copy images from one folder to another using their names on a pandas dataframe 仅将两列从一个DataFrame复制到Pandas中的另一列 - Copy just two columns from one DataFrame to another in pandas 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas Pandas 将列名从一个数据帧复制到另一个 - Pandas copy column names from one dataframe to another 将行从 dataframe 复制到 pandas 中的另一个 dataframe - Copy rows from a dataframe to another dataframe in pandas 在 Pandas Dataframe 中按一列排序,然后按另一列分组? - Sort by one column, then group by another, in Pandas Dataframe? 在一列上按另一列对数据框进行排序-Pandas - Sort dataframe by another on one column - pandas 如何将一行从一个熊猫数据帧复制到另一个熊猫数据帧? - How do I copy a row from one pandas dataframe to another pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM