0
我有一個數據框,其中某些行包含特殊字符'#'。如何在行中存在某些字符時獲取熊貓數據框中的子字符串?
這是我的數據,我能找到的「#」索引的位置:
import pandas as pd
df = pd.DataFrame(data=['fig#abc', 'strawberry', 'applepie#efg'], columns=['fruitname'])
ind= df.fruitname.str.find("#")
df['col1'].str.find(".")-1]
print df
print ind
fruitname
0 fig#abc
1 strawberry
2 applepie#efg
0 3
1 -1
2 8
我想與之前「#」前幾個字符的值的新列的數據,如果索引「# '是大於4,其它方式與原始數據的價值:
fruitname_new
0 fig#abc
1 strawberry
2 applepie
什麼是得到這樣的結果的最佳方式?