小编lad*_*bug的帖子

Groupby 熊猫中字符串的一部分

我正在尝试通过将字符串的一部分分组到列中来构建新的数据框。

import pandas

df = pandas.DataFrame([{'A': 'string_300_bla1', 'B': "Hi", 'C': 3},
                       {'A': 'string_300_blaa2', 'B': "Hello", 'C': 4},
                       {'A': 'string_487_blaaa1', 'B': "nice", 'C': 9},
                       {'A': 'string_487_blaaa2', 'B': "day", 'C': 6}])
Run Code Online (Sandbox Code Playgroud)

我想从字符串的这一部分创建一个 groupby

字符串_ 300 _bla1

我试过:

import re

dfs = df['A'].str.contains('.*_\d+_.*', re.IGNORECASE).groupby(df['B'])
Run Code Online (Sandbox Code Playgroud)

我的输出:

<pandas.core.groupby.generic.SeriesGroupBy object at 0x00000279EFD009E8>
Run Code Online (Sandbox Code Playgroud)

良好的输出:

dfs = pandas.DataFrame([{'A': 'string_300', 'B': "Hi\n\nHello"},
                       {'A': 'string_487', 'B': "nice\n\nday"}])
Run Code Online (Sandbox Code Playgroud)

python group-by pandas

5
推荐指数
1
解决办法
41
查看次数

迭代嵌套列表并对每个元素进行操作

我有一个列表列表,我正在尝试删除所有非字母字符。

\n\n

我尝试使用isalpha()

\n\n
data = [\n    ['we', '\\n\\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best'], \n    ['though', '10/3/2011', 'it', 'may', 'not', '\\n\\n', 'make', 'landfall', 'all', 'week', '2 000 \xe2\x82\xac', 'if', 'it', 'follows', 'that', '\xe2\x80\xa2', 'track'],\n    ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '\xe2\x80\xa2', 'floods', '\\n\\n', 'are', 'possible'],\n]\n\nnew_data = ''.join([i for i in data if i.isalpha()])\n
Run Code Online (Sandbox Code Playgroud)\n\n

预期输出:

\n\n
['we will pray and hope for the best', \n 'though it may not make landfall all week if it …
Run Code Online (Sandbox Code Playgroud)

python replace list

1
推荐指数
1
解决办法
468
查看次数

标签 统计

python ×2

group-by ×1

list ×1

pandas ×1

replace ×1