简体   繁体   English

Python 或 Excel:如何比较 2 列,然后在新列中写入第 3 列的值?

[英]Python or Excel: How can you compare 2 columns and then write the value of a 3rd column in a new column?

I have 2 tables that look like this:我有 2 个看起来像这样的表:

Table 1表格1

| ID | Tel. |   Name  |
|:--:|:----:|:-------:|
| 1  | 1234 | Denis   |
| 2  | 4567 | Michael |
| 3  | 3425 | Peter   |
| 4  | 3242 | Mary    |

Table 2表 2

| ID | Contact Date |
|:--:|:------------:|
| 1  | 2014-05-01   |
| 2  | 2003-01-05   |
| 3  | 2020-01-10   |
| 4  | NULL         |

Now I want to Compare the First Table with the second table with the ID column to look if contact 1 is already in the list where people were contacted.现在我想比较第一个表和带有ID列的第二个表,以查看contact 1是否已经在contact 1列表中。 After this, I want to write the Contact Date into the first table to see the last contact date in the main table.在此之后,我想将Contact Date写入第一个表中以查看主表中的最后一个联系日期。

How would I do this?我该怎么做?

Thanks for any answers!!感谢您的任何答案!

Here's a solution that might get your interest using MySQL ;这是一个使用MySQL可能会引起您兴趣的解决方案; Then if you want to implement it into Python will be easy.那么如果你想将它实现到Python 中会很容易。

First, Let's create our T1 Table AS CODED首先,让我们创建我们的T1AS CODED

create table T1(
    id integer primary key,
    tel integer, 
    name varchar(100)
    );

create table T2(
    id integer primary key,
    contactDate date
    );

Second Table T2第二桌T2

create table T2(
    id integer primary key,
    contactDate date
    );

Insertion Of T1 AND T2 : T1T2插入:

-- Table 'T1' Insertion

insert into T1(id, tel, name) VALUES 
    (1, 1234, "Denis"),
    (2, 4567, "Michael"),
    (3, 3425,"Peter"),
    (4, 3242, "Mary");

-- Table 'T2' Insertion              
              
insert into T2(id, contactDate) VALUES 
    (1, 20140105),
    (2, 20030105),
    (3, 20201001),
    (4, Null);    

Then, Create T3 With the Select Statement for both tables using INNER JOIN For Joins Results然后,使用INNER JOIN For Joins Results 为两个表创建带有 Select 语句的T3内部联接

CREATE TABLE T3 AS
    SELECT T1.id, T1.name, T1.tel, T2.contactDate
    FROM T1
    INNER JOIN T2 ON T1.id=T2.id;

Then, SELECT to check the results然后, SELECT检查结果

select * from T3;

OUTPUT输出

| id |   name  | tel  | contactDate |
|:--:|:-------:|------|-------------|
| 1  | Denis   | 1234 | 2014-01-05  |
| 2  | Michael | 4567 | 2003-01-05  |
| 3  | Peter   | 3425 | 2020-10-01  |
| 4  | Mary    | 3242 | NULL        |

I Hope This Helps.我希望这有帮助。 I was really trying to merge the T3 ---> contactDate with T1 for like 3 hours.我真的想将T3 ---> contactDateT1合并 3 个小时。 But it was process heavy.但过程繁重。 I will attach Links that could help you more.我会附上可以帮助你更多的链接。

Reference参考

INNER JOIN内部联接

SQL ALTER TABLE Statement SQL ALTER TABLE 语句

SQL Server INSERT Multiple Rows SQL Server 插入多行

INSERT INTO SELECT statement overview and examples INSERT INTO SELECT 语句概述和示例

SQL INSERT INTO SELECT Statement SQL INSERT INTO SELECT 语句

暂无
暂无

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

相关问题 Python 比较 2 列并用第 3 列中的值写入第 4 列(熊猫) - Python Compare 2 Columns And Write A 4th Column With Values From 3rd Column (pandas ) 如何比较两列并获取 python pandas dataframe 中两列中所有匹配项的第三列的平均值? - how to compare two columns and get the mean value of the the 3rd column for all matching items in the two in python pandas dataframe? Python Pandas - 检查两列中的值,对第三列求和 - Python Pandas - check value in two columns, sum the 3rd column 是否有一种SQL语法会根据同一表中第3列的相等值搜索2列来创建新列? - Is there a SQL syntax that will create new column by searching in 2 columns based on equal value of 3rd column in same table? 比较两个日期列 - 检查它们是否在范围内 - 从第三列取值 - compare two date columns - check if they fall in range - take value from 3rd column 第 3 列 pandas python 中至少有两列 - Minimum of two columns in a 3rd column pandas python 基于第三个值的 New_target 列 - New_target column based on 3rd value 如何根据第一列和第二列之间的差异在 dataframe 的第三列上获取值? - How to get a value on a 3rd column in dataframe depending on the difference between the first and second columns? 如何使用其他两列作为轴 plot 第 3 列 - How to plot the 3rd column using the other two columns as axes 根据 Python 中的第三列值设置 Plotly 条形颜色 - Set Plotly Bar Color Based on 3rd Column Value in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM