[英]How to extract entire part of string after certain character in dataframe column?
I am working on using the below code to extract the last number of pandas dataframe column name. 我正在使用下面的代码来提取最后一个pandas dataframe列名。
names = df.columns.values
new_df = pd.DataFrame()
for name in names:
if ('.value.' in name) and df[name][0]:
last_number = int(name[-1])
print(last_number)
key, value = my_dict[last_number]
try:
new_df[value][0] = list(new_df[value][0]) + [key]
except:
new_df[value] = [key]
name
is a string that looks like this: name
是一个如下所示的字符串:
'data.answers.1234567890.value.0987654321'
I want to take the entire number after .value.
我想在
.value.
之后取整个数字.value.
as in the IF
statement. 如在
IF
声明中。 How would do this in the IF
statement above? 在上面的
IF
语句中如何做到这一点?
Use str.split
, and extract the last slice with -1
(also gracefully handles false cases): 使用
str.split
,并使用-1
提取最后一个切片(也正常处理错误情况):
df = pd.DataFrame(columns=[
'data.answers.1234567890.value.0987654321', 'blahblah.value.12345', 'foo'])
df.columns = df.columns.str.split('value.').str[-1]
df.columns
# Index(['0987654321', '12345', 'foo'], dtype='object')
Another alternative is splitting inside a listcomp: 另一种方法是在listcomp中拆分:
df.columns = [x.split('value.')[-1] for x in df.columns]
df.columns
# Index(['0987654321', '12345', 'foo'], dtype='object')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.