我当前有以下数据帧:
df1
3 4 5 6
0 NaN NaN Sea NaN
1 light medium light medium
2 26 41.5 15 14
3 32 40 18 29
4 41 29 19 42
我正在尝试返回一个新的数据帧,其中只保留了海柱和以后的数据帧:
df1
5 6
0 Sea NaN
1 light medium
2 15 14
3 18 29
4 19 42
我觉得我很熟悉我的代码:
for i in range(len(df.columns)):
if pd.Series.any(df.iloc[:,i].str.contains(pat="Sea")):
xyz = df.columns[i] #This is the piece of code I am having trouble with
df = df.loc[:,[xyz:??]]
从本质上说,我想返回单词“Sea”所在的列索引,然后根据该索引的长度创建一个新的dataframe。希望这个解释有道理,并感谢任何帮助
也许你可以写一个简单的函数来实现。你知道吗
既然如此,cᴏʟᴅsᴘᴇᴇᴅ's answer应该是首选,因为它避免了类似于此函数的列循环。你知道吗
您可以使用
list
和index
来尝试这一点步骤1:获取列名:
步骤2:使用
df.loc
索引:如果
df.loc[:, c:]
不起作用,您可能需要使用更明确的版本(感谢piRSquared的简化):相关问题 更多 >
编程相关推荐