我有文本文件要加载到数据框中。加载后,这些值都在一列中,格式如下:
0 Alabama[edit]
1 Auburn (something somethign)
2 Florence (something somethign)
.
.
.
12 California[edit]
13 Angwin (something something)
14 Arcata (something something)
我必须将这些值分为两列:State 和 RegionName。
State 应该是索引
所有州名称都有 [edit] 后缀,地区名称末尾有 (....)。 在清理数据之前,我想我可以使用 [edit] 和 (..) 作为掩码。
我试图将两个“值”分开
df=pd.read_table("file.txt", names=["State","RegionName])
state=df[df["State"].str.contains(r"\[edit\]")]
region=df[df["State"].str.contains(r"\s+\(.*\)")]
并尝试以某种方式合并这些,但没有运气并且 如果我尝试使用状态和区域来创建新的 df,我会收到索引错误
我尝试使用.str.extract
df.row.str.extract("(?P<State>\r\[\edit\]")
但我收到一条错误消息,说 df 现在具有 .row(or.str) 属性,并且我确信该模式也是错误的。
任何帮助将不胜感激。
感谢和问候
最佳答案
类似这样的吗?
df['state'] = np.where(df.place.str.contains('edit'), df.place, np.nan)
df['region'] = np.where(df.place.str.contains('\('), df.place, np.nan)
df.drop('place', 1, inplace =True)
df['state'].ffill(inplace = True)
df.set_index('state', inplace = True)
region
state
Alabama[edit] NaN
Alabama[edit] Auburn (something somethign)
Alabama[edit] Florence (something somethign)
California[edit] NaN
California[edit] Angwin (something something)
California[edit] Arcata (something something)
关于python - 如何使用(最好)正则表达式模式将一列中的值拆分为两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47101613/