简体   繁体   English

使用 Pandas 合并 tsv 文件时遇到问题

[英]Trouble with merging tsv files using pandas

So I have been having an issue using pandas where it simply does not merge certain lines.所以我在使用熊猫时遇到了一个问题,它根本不合并某些行。 For example, when trying to merge the following two excerpts together:例如,当尝试将以下两个摘录合并在一起时:

Haggai  1:1 In the second year of Darius the king, in the sixth month, in the first day of the month, the Word of Yahweh came by Haggai, the prophet, to Zerubbabel, the son of Shealtiel, governor of Judah, and to Joshua, the son of Jehozadak, the high priest, saying,
Haggai  1:2 "This is what Yahweh of Hosts says: These people say, 'The time hasn't yet come, the time for Yahweh's house to be built.'"
Haggai  1:3 Then the Word of Yahweh came by Haggai, the prophet, saying,
Haggai  1:4 "Is it a time for you yourselves to dwell in your paneled houses, while this house lies waste?
Haggai  1:5 Now therefore this is what Yahweh of Hosts says: Consider your ways.
Haggai  1:6 You have sown much, and bring in little. You eat, but you don't have enough. You drink, but you aren't filled with drink. You clothe yourselves, but no one is warm, and he who earns wages earns wages to put them into a bag with holes in it."

and

Haggai  1:1 ΕΝ τῷ δευτέρῳ ἔτει ἐπὶ Δαρίου τοῦ βασιλέως ἐν τῷ μηνὶ τῷ ἕκτῳ μιᾷ τοῦ μηνὸς ἐγένετο λόγος Κυρίου ἐν χειρὶ Ἀγγαίου τοῦ προφήτου λέγων Εἰπὸν πρὸς Ζοροβάβελ τὸν τοῦ Σαλαθιὴλ ἐκ φυλῆς Ἰούδα καὶ πρὸς Ἰησοῦν τὸν τοῦ Ἰωσεδὲκ τὸν ἱερέα τὸν μέγαν λέγων
Haggai  1:2 Τάδε λέγει Κύριος Παντοκράτωρ λέγων Ὁ λαὸς οὗτος λέγουσιν Οὐχ ἤκει ὁ καιρὸς τοῦ οἰκοδομῆσαι τὸν οἶκον Κυρίου.
Haggai  1:3 καὶ ἐγένετο λόγος Κυρίου ἐν χειρὶ Ἀγγαίου τοῦ προφήτου λέγων
Haggai  1:4 Εἰ καιρὸς μέν ὑμῖν ἐστιν τοῦ οἰκεῖν ἐν οἴκοις ὑμῶν κοιλοστάθμοις, ὁ δὲ οἶκος ὑμῶν ἐξηρήμωται;
Haggai  1:5 καὶ νῦν τάδε λέγει Κύριος Παντοκράτωρ Τάξατε δὴ τὰς καρδίας ὑμῶν εἰς τὰς ὁδοὺς ὑμῶν·
Haggai  1:6 ἐσπείρατε πολλὰ καὶ εἰσηνέγκατε ὀλίγα, ἐφάγετε καὶ οὐκ εἰς πλησμονήν, ἐπίετε καὶ οὐκ εἰς μέθην, περιεβάλεσθε καὶ οὐκ ἐθερμάνθητε ἐν αὐτοῖς, καὶ ὁ τοὺς μισθοὺς συνάγων συνήγαγεν εἰς δεσμὸν τετρυπημένον.

I get:我得到:

21  Haggai  1:1 ΕΝ τῷ δευτέρῳ ἔτει ἐπὶ Δαρίου τοῦ βασιλέως ἐν τῷ μηνὶ τῷ ἕκτῳ μιᾷ τοῦ μηνὸς ἐγένετο λόγος Κυρίου ἐν χειρὶ Ἀγγαίου τοῦ προφήτου λέγων Εἰπὸν πρὸς Ζοροβάβελ τὸν τοῦ Σαλαθιὴλ ἐκ φυλῆς Ἰούδα καὶ πρὸς Ἰησοῦν τὸν τοῦ Ἰωσεδὲκ τὸν ἱερέα τὸν μέγαν λέγων In the second year of Darius the king, in the sixth month, in the first day of the month, the Word of Yahweh came by Haggai, the prophet, to Zerubbabel, the son of Shealtiel, governor of Judah, and to Joshua, the son of Jehozadak, the high priest, saying,
22  Haggai  1:2 Τάδε λέγει Κύριος Παντοκράτωρ λέγων Ὁ λαὸς οὗτος λέγουσιν Οὐχ ἤκει ὁ καιρὸς τοῦ οἰκοδομῆσαι τὸν οἶκον Κυρίου.   This is what Yahweh of Hosts says: These people say, 'The time hasn't yet come, the time for Yahweh's house to be built.'
23  Haggai  1:3 καὶ ἐγένετο λόγος Κυρίου ἐν χειρὶ Ἀγγαίου τοῦ προφήτου λέγων    Then the Word of Yahweh came by Haggai, the prophet, saying,
24  Haggai  1:4 Εἰ καιρὸς μέν ὑμῖν ἐστιν τοῦ οἰκεῖν ἐν οἴκοις ὑμῶν κοιλοστάθμοις, ὁ δὲ οἶκος ὑμῶν ἐξηρήμωται;   "Is it a time for you yourselves to dwell in your paneled houses, while this house lies waste?
Haggai  1:5 Now therefore this is what Yahweh of Hosts says: Consider your ways.
Haggai  1:6 You have sown much, and bring in little. You eat, but you don't have enough. You drink, but you aren't filled with drink. You clothe yourselves, but no one is warm, and he who earns wages earns wages to put them into a bag with holes in it."

Where clearly Haggai 1:5 and Haggai 1:6 do not merge correctly.显然Haggai 1:5Haggai 1:6没有正确合并。

The code I am using is:我正在使用的代码是:

import pandas as pd

df1 = pd.read_table('greekBible.txt')
df2 = pd.read_table('englishBible.txt')

df3 = pd.merge(df1, df2, on=['Book', 'Chapter:Verse'])

df3.to_csv('test.txt', sep="\t")

Keep in mind, this is only a small excerpt.请记住,这只是一小部分摘录。 Further, the two bibles are not totally aligned — there are entries in one that are not in the other and vice versa.此外,两本圣经并不完全一致——一本中有条目,另一本没有,反之亦然。 However, this shouldn't be a problem to my understanding.但是,这对我的理解应该不是问题。

Thank you very much for your help with this problem!非常感谢您对这个问题的帮助!

There appear to be missing quotes in your file, probably due to how the text was split.您的文件中似乎缺少引号,可能是由于文本的拆分方式。 It is most obvious on the line Haggai 1:4 "Is it a time for you yourselves to dwell in your paneled houses, while this house lies waste?哈该书 1:4 这句话最明显:“你们自己住在镶板的房子里,而这房子却荒废吗?

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

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