为了账号安全,请及时绑定邮箱和手机立即绑定

python数据表,列上的字符串操作

python数据表,列上的字符串操作

梦里花落0921 2023-10-26 15:15:37
from datatable import dt, f, g, by, update, join, sorttt = dt.Frame({'a' : ['A1','A2','A3'], 'b':[100,200,300]})print(tt)   | a     b-- + --  --- 0 | A1  100 1 | A2  200 2 | A3  300[3 rows x 2 columns]如何删除a列中的“A”并将其作为数据表中的数字分配给新列“c”(即没有熊猫)?在以下的帮助下它看起来像这样pandastt['c'] = tt.to_pandas()['a'].str.replace('A','').astype(int)数据表本机版本不太有效tt[:, update(c = [int(x.replace('A','')) for x in f.a])]TypeError: 'datatable.FExpr' object is not iterable顺便问一下,对于 python pandas 和 R data.table 的频繁用户,是否有一本高级/完整的食谱可以帮助从 R data.table 过渡到 py-datatable?网站上有一个页面,但还不够。
查看完整描述

3 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

这是一个扩展性不是很好的 hack:

第 1 步:将a列转储到本机 python 中并创建一个值元组:

tuples = [(entry[0], entry[-1]) for entry  in tt['a'].to_list()[0]]

第 2 步:cbind回框架tt

tt.cbind(dt.Frame(tuples))


tt



    a   b   C0  C1

0   A1  100 A   1

1   A2  200 A   2

2   A3  300 A   3

如果你只需要 A,那么你可以使用下面的代码,它仍然不能很好地扩展(想象你的列中有空值),并且很粗糙(我们必须索引到列表中才能得到我们想要的) :


tt["A_only"] = dt.Frame([entry[0] for entry in tt['a'].to_list()[0]])


tt


     a   b   A_only

0   A1  100     A

1   A2  200     A

2   A3  300     A

如前所述,这不能很好地扩展。此外,它没有提供数据表所期望的速度。


目前,数据表没有很好的字符串操作支持(我相信库维护者目前正在努力解决这个问题,以及其他一些要求的功能)


查看完整回答
反对 回复 2023-10-26
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

我知道这是一个老问题,但万一有人仍在寻找这个问题——在刚刚发布的 1.0.0 中,可以执行以下操作:


tt = dt.Frame({'a' : ['A1','A2','A3'], 'b':[100,200,300]})

tt["A_only"] = tt[:, f.a[0:1]]

tt["num_only"] = tt[:, f.a[1:]]

tt["num_only"] = dt.Type.int8  # Change the type of the column to `int`

tt.ltypes

上面使用了字符串列上的切片,即依赖于固定格式。还有.re正则表达式的部分,但我只看到match,没有看到extract。


查看完整回答
反对 回复 2023-10-26
?
三国纷争

TA贡献1804条经验 获得超7个赞

这是我为了得到你想要的东西而做的一个技巧。我仍在学习数据表,所以请耐心等待我完全进入它。


首先,将数据表转换为数据框。执行我前面列出的操作,然后将数据帧转换回数据表。Walla,您现在拥有一个包含所需结果的数据表。


我就是这样做的。


from datatable import dt, f, g, by, update, join, sort

tt = dt.Frame({'a' : ['A1','A2','A3'], 'b':[100,200,300]})

df = tt.to_pandas()

df = df.join(df.a.str.extract('([a-zA-Z])([0-9])', expand=True).add_prefix('a'))

df = df.rename(columns = {'a0': 'c', 'a1': 'd'})

tt = dt.Frame(df)

tt

其输出将是:

https://img1.sycdn.imooc.com/653a124100010c2a05720364.jpg

您可以拆分列并重命名字段。


import pandas as pd

df = pd.DataFrame({'a' : ['A1','A2','A3'], 'b':[100,200,300]})

print (df)

df = df.join(df['a'].str.split(r'(\d.*)', expand=True).add_prefix('a'))

df.drop('a2',axis = 1,inplace=True)

df = df.rename(columns = {'a0': 'c', 'a1': 'd'})

print (df)

输出将是:


初始数据框将是:


    a    b

0  A1  100

1  A2  200

2  A3  300

新的 DataFrame 将如下所示:


    a    b  c  d

0  A1  100  A  1

1  A2  200  A  2

2  A3  300  A  3

或者,您也可以使用extract正则表达式来完成此操作。


import pandas as pd

df1 = pd.DataFrame({'a' : ['A1','A2','A3'], 'b':[100,200,300]})

df1 = df1.join(df1.a.str.extract('([a-zA-Z])([0-9])', expand=True).add_prefix('a'))

df1 = df1.rename(columns = {'a0': 'c', 'a1': 'd'})

print (df1)

它会给你相同的结果:


    a    b

0  A1  100

1  A2  200

2  A3  300

在此选项中,它不会创建需要删除的附加列


    a    b  c  d

0  A1  100  A  1

1  A2  200  A  2

2  A3  300  A  3


查看完整回答
反对 回复 2023-10-26
  • 3 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信