![](/img/trans.png)
[英]Python Compare 2 Columns And Write A 4th Column With Values From 3rd Column (pandas )
[英]Python or Excel: How can you compare 2 columns and then write the value of a 3rd column in a new column?
我有 2 個看起來像這樣的表:
表格1
| ID | Tel. | Name |
|:--:|:----:|:-------:|
| 1 | 1234 | Denis |
| 2 | 4567 | Michael |
| 3 | 3425 | Peter |
| 4 | 3242 | Mary |
表 2
| ID | Contact Date |
|:--:|:------------:|
| 1 | 2014-05-01 |
| 2 | 2003-01-05 |
| 3 | 2020-01-10 |
| 4 | NULL |
現在我想比較第一個表和帶有ID
列的第二個表,以查看contact 1
是否已經在contact 1
列表中。 在此之后,我想將Contact Date
寫入第一個表中以查看主表中的最后一個聯系日期。
我該怎么做?
感謝您的任何答案!
這是一個使用MySQL可能會引起您興趣的解決方案; 那么如果你想將它實現到Python 中會很容易。
首先,讓我們創建我們的T1
表AS CODED
create table T1(
id integer primary key,
tel integer,
name varchar(100)
);
create table T2(
id integer primary key,
contactDate date
);
第二桌T2
create table T2(
id integer primary key,
contactDate date
);
T1
和T2
插入:
-- 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);
然后,使用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;
然后, SELECT
檢查結果
select * from T3;
輸出
| 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 |
我希望這有幫助。 我真的想將T3
---> contactDate
與T1
合並 3 個小時。 但過程繁重。 我會附上可以幫助你更多的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.