我正在尝试通过将字符串的一部分分组到列中来构建新的数据框。
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) 我有一个列表列表,我正在尝试删除所有非字母字符。
\n\n我尝试使用isalpha()
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()])\nRun 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)