python两列字符串合并_python数据框 - 将字符串列拆分为两列(python data frames - splitting string column into two columns)...

python数据框 - 将字符串列拆分为两列(python data frames - splitting string column into two columns)

我正在玩Whatsapp的历史聊天。 我想将消息列拆分为两列 - 时间和消息。

为了用分隔符“ - ”拆分两个,我尝试了:

history['message'] = pd.DataFrame([line.split(" - ",1) for line in history['message']])

但历史['消息']成为时间。

我不明白为什么,因为line.split(“ - ”,1)假设最多给出2个元素的列表。

I'm playing with Whatsapp's history chat. I want to split the message column into two columns - time and message.

In order to split the two with the delimiter " - " I tried:

history['message'] = pd.DataFrame([line.split(" - ",1) for line in history['message']])

But history['message'] becomes the time only.

I don't understand why, because the line.split(" - ", 1) supposes to give a list of 2 elements at most.

原文:https://stackoverflow.com/questions/46347100

2019-12-10 04:15

满意答案

我认为你需要str.split和expand=True来返回DataFrame :

history = pd.DataFrame({'message':['a - b','c - d - r']})

history[['a','b']] = history['message'].str.split(' - ', n=1, expand=True)

print (history)

message a b

0 a - b a b

1 c - d - r c d - r

如果没有NaNs使用:

history[['a','b']] = pd.DataFrame([line.split(" - ", 1) for line in history['message']])

对我来说,返回错误:

history['a'] = pd.DataFrame([line.split(" - ", 1) for line in history['message']])

print (history)

ValueError:传递的项目数量错误2,位置意味着1

因此,如果你工作,尝试检查分隔符,因为它似乎没有split :

样品:

history['a'] = history['message'].str.split('^', n=1, expand=True)

print (history)

message a

0 a - b a - b

1 c - d - r c - d - r

I think you need str.split with expand=True for return DataFrame:

history = pd.DataFrame({'message':['a - b','c - d - r']})

history[['a','b']] = history['message'].str.split(' - ', n=1, expand=True)

print (history)

message a b

0 a - b a b

1 c - d - r c d - r

If no NaNs use:

history[['a','b']] = pd.DataFrame([line.split(" - ", 1) for line in history['message']])

For me return error:

history['a'] = pd.DataFrame([line.split(" - ", 1) for line in history['message']])

print (history)

ValueError: Wrong number of items passed 2, placement implies 1

So if for you it working, try check separator, because it seems there is no split:

Sample:

history['a'] = history['message'].str.split('^', n=1, expand=True)

print (history)

message a

0 a - b a - b

1 c - d - r c - d - r

2017-09-21

相关问答

尝试这个。 你也必须施放i.Answer。 ;WITH SplitValues (ID, OriginalValue, SplitValue, Level)

AS

(

SELECT 'RN110455', cast('very satisfied very satisfied very satisfied very satisfied very satisfied' as varchar(max))

, CAST('' AS VARCHAR(MAX)), 0

UNIO...

你可以使用strsplit 。 将空字符串作为分隔符传递会导致每个字母分割。 a <- c("A1", "B1", "C2", "D5", "R3")

strsplit(a, "")

[[1]]

[1] "A" "1"

[[2]]

[1] "B" "1"

[[3]]

[1] "C" "2"

[[4]]

[1] "D" "5"

[[5]]

[1] "R" "3"

如果你想把它放在一个矩阵中 > do.call(rbind, strsplit(a, ""))

[,1] [,2]...

假设您的第二列不包含任何空格,我们可以搜索最后一个空格的位置,然后在那里拆分: SELECT Klubs,

substr(Klubs, 1, char_length(Klubs) - instr(reverse(Klubs),' ')) as Region,

substr(Klubs, char_length(Klubs) - instr(reverse(Klubs),' ')+2) as Club

FROM ML;

我用instr搜索空格,为了反过来我首先在字符串上使用reverse函数...

我认为你需要str.split和expand=True来返回DataFrame : history = pd.DataFrame({'message':['a - b','c - d - r']})

history[['a','b']] = history['message'].str.split(' - ', n=1, expand=True)

print (history)

message a b

0 a - b a b

1 c - d - r ...

我创建了一个测试代码,打印出你想要的东西。 我们的想法是使用str的numpy矩阵来存储值。 矩阵充满了“NA”,因此它们将在最后。 比这个技巧使用广播来复制所需位置的值。 通过连接和删除不需要的列来完成整个数据框。 代码迭代字典的键。 我假设您正在使用pandas数据帧,并且加载的二进制值被解释为object s。 代码的第一部分是构造数据帧字典的头。 import pandas as pd

import numpy as np

## Lets call it "header"

from i...

您可以正确指定目标列: library(tidyr)

separate(DF, V1, paste0("X",1:8), sep="-")

这使: X1 X2 X3 X4 X5 X6 X7 X8

1 Place1 Place2 Place2 Place4 Place2 Place3 Place5

2 Place7 Place7 Place7 Place7 Place7 Place7 Place7 Place7

3 ...

这是一个使用字典理解然后是pd.concat解决方案: str1 = ';src=4457426;type=be_salec;cat=be_thnky;qty=1;cost=60.00;ord=50608803;gtm=G64;gcldc=*;gclaw=*;gac=UA-32723457-1:*;u1=men;u2=schoenen;u3=none;u5=VA38G1NRI;u6=80;u7=0;u8=1;u9=EUR;u10=be;u11=Suede Old Skool Shoes;u12=c...

将这些数据拆分成单独的列是一个非常好的开始(昏迷分离的值是异端)。 但是,“可变数量的属性”通常应该被建模为一对多关系 。 CREATE TABLE main_entity (

id INT PRIMARY KEY,

other_fields INT

);

CREATE TABLE entity_properties (

main_entity_id INT PRIMARY KEY,

property_value INT,

FOREIGN KEY (main_entity_i...

所有“排列良好”的列是固定宽度数据的典型特征。 为了这个答案,我把你的三行数据和一行标题信息写到一个名为“x”的临时文件中。 对于实际使用,将“x”替换为文件名/路径,就像通常与read.delim使用。 这是示例数据: x <- tempfile()

cat("Name Country Age\nJohn,Smith United Kingdom 20\nWashington,George USA ...

一种可能的解决方 我不完全确定你想要用字符串中的'x'做什么,我把它们保存在连接键中,但是通过将\\1\\2更改为\\1你只保留第一个字母。 a <- data.frame(

Name = paste0(".", c("tony", "tom", "foo", "bar", "foobar"), ".x.rds"),

Numbers = rnorm(5)

)

b <- data.frame(

Name = paste0(c("tony", "tom", "bar", "foobar"...

相关文章

Data Week: Becoming a data scientist Data Pointed,

...

solr 的 data import 导入 mysql数据 (1)、编辑 example/solr/c

...

Spring Data: a new perspective of data operations

...

Java String类 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提

...

清明假期翻以前的笔记发现有一些NoSQL相关的内容,比较零散,是之前读《Big Data Glossa

...

今天遇见一个问题.不知道怎么解决. 如: 自己建立了一个文件read.txt 里面存放这样的 键值

...

Python 字符串操作,字符串序列用于表示和存储文本,python中字符串是不可变的,一旦声明,不能

...

1. 要求 将数据库中的数据直接创建到Solr索引中去。先做全部索引,然后定期做增量索引。 2. 环

...

字符串的格式化 在python中也有类似于c中的printf()的格式输出标记。在python中格式化

...

中文名: Oracle Streams 11g数据复制 原名: Oracle Streams 1

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

你可能感兴趣的:(python两列字符串合并)