我有一个带有列名的数据框架,我想找到包含某个字符串但不完全匹配的数据框架。我正在搜索列名中的“spike”,如“spike-2”,“嘿spike”,“spike- in”(“spike”部分总是连续的)。
我希望列名作为字符串或变量返回,所以我稍后使用df['name']或df[name]正常访问该列。我试过想办法做到这一点,但没有用。任何建议吗?
我有一个带有列名的数据框架,我想找到包含某个字符串但不完全匹配的数据框架。我正在搜索列名中的“spike”,如“spike-2”,“嘿spike”,“spike- in”(“spike”部分总是连续的)。
我希望列名作为字符串或变量返回,所以我稍后使用df['name']或df[name]正常访问该列。我试过想办法做到这一点,但没有用。任何建议吗?
当前回答
根据开始、包含和结束获取名称和子设置:
# from: https://stackoverflow.com/questions/21285380/find-column-whose-name-contains-a-specific-string
# from: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html
# from: https://cmdlinetips.com/2019/04/how-to-select-columns-using-prefix-suffix-of-column-names-in-pandas/
# from: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.filter.html
import pandas as pd
data = {'spike_starts': [1,2,3], 'ends_spike_starts': [4,5,6], 'ends_spike': [7,8,9], 'not': [10,11,12]}
df = pd.DataFrame(data)
print("\n")
print("----------------------------------------")
colNames_contains = df.columns[df.columns.str.contains(pat = 'spike')].tolist()
print("Contains")
print(colNames_contains)
print("\n")
print("----------------------------------------")
colNames_starts = df.columns[df.columns.str.contains(pat = '^spike')].tolist()
print("Starts")
print(colNames_starts)
print("\n")
print("----------------------------------------")
colNames_ends = df.columns[df.columns.str.contains(pat = 'spike$')].tolist()
print("Ends")
print(colNames_ends)
print("\n")
print("----------------------------------------")
df_subset_start = df.filter(regex='^spike',axis=1)
print("Starts")
print(df_subset_start)
print("\n")
print("----------------------------------------")
df_subset_contains = df.filter(regex='spike',axis=1)
print("Contains")
print(df_subset_contains)
print("\n")
print("----------------------------------------")
df_subset_ends = df.filter(regex='spike$',axis=1)
print("Ends")
print(df_subset_ends)
其他回答
这个答案使用了DataFrame。过滤器方法来做这个没有列表理解:
import pandas as pd
data = {'spike-2': [1,2,3], 'hey spke': [4,5,6]}
df = pd.DataFrame(data)
print(df.filter(like='spike').columns)
只输出'spike-2'。你也可以使用regex,正如一些人在上面的评论中建议的那样:
print(df.filter(regex='spike|spke').columns)
将输出两个列:['spike-2', 'hey spke']
根据开始、包含和结束获取名称和子设置:
# from: https://stackoverflow.com/questions/21285380/find-column-whose-name-contains-a-specific-string
# from: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html
# from: https://cmdlinetips.com/2019/04/how-to-select-columns-using-prefix-suffix-of-column-names-in-pandas/
# from: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.filter.html
import pandas as pd
data = {'spike_starts': [1,2,3], 'ends_spike_starts': [4,5,6], 'ends_spike': [7,8,9], 'not': [10,11,12]}
df = pd.DataFrame(data)
print("\n")
print("----------------------------------------")
colNames_contains = df.columns[df.columns.str.contains(pat = 'spike')].tolist()
print("Contains")
print(colNames_contains)
print("\n")
print("----------------------------------------")
colNames_starts = df.columns[df.columns.str.contains(pat = '^spike')].tolist()
print("Starts")
print(colNames_starts)
print("\n")
print("----------------------------------------")
colNames_ends = df.columns[df.columns.str.contains(pat = 'spike$')].tolist()
print("Ends")
print(colNames_ends)
print("\n")
print("----------------------------------------")
df_subset_start = df.filter(regex='^spike',axis=1)
print("Starts")
print(df_subset_start)
print("\n")
print("----------------------------------------")
df_subset_contains = df.filter(regex='spike',axis=1)
print("Contains")
print(df_subset_contains)
print("\n")
print("----------------------------------------")
df_subset_ends = df.filter(regex='spike$',axis=1)
print("Ends")
print(df_subset_ends)
# select columns containing 'spike'
df.filter(like='spike', axis=1)
也可以按名称、正则表达式选择。参考:pandas.DataFrame.filter
另一个解决方案是返回df的一个子集,其中包含所需的列:
df[df.columns[df.columns.str.contains("spike|spke")]]
只需要迭代DataFrame。列,现在这是一个例子,在这个例子中,你将以一个匹配的列名列表结束:
import pandas as pd
data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)
spike_cols = [col for col in df.columns if 'spike' in col]
print(list(df.columns))
print(spike_cols)
输出:
['hey spke', 'no', 'spike-2', 'spiked-in']
['spike-2', 'spiked-in']
解释:
df。Columns返回列名列表 [col为df中的col。列如果'spike'在col]迭代列表df。如果col包含'spike',则将其添加到结果列表中。这个语法是列表理解。
如果你只想要匹配的列的结果数据集,你可以这样做:
df2 = df.filter(regex='spike')
print(df2)
输出:
spike-2 spiked-in
0 1 7
1 2 8
2 3 9