我有一個df,其中包含一列字符串和一個接受字符串的函數。我想通過df插入該列中的值而不輸入它,那麼最好的方法是什麼?我看着適用,但它似乎並不適用於這種情況。將來自熊貓數據框列的所有值插入到函數中
編輯:
df = A B C
1 2 "cheese"
2 4 "spiders"
def samplefunc(item):
print('i hate ' + item)
我有一個df,其中包含一列字符串和一個接受字符串的函數。我想通過df插入該列中的值而不輸入它,那麼最好的方法是什麼?我看着適用,但它似乎並不適用於這種情況。將來自熊貓數據框列的所有值插入到函數中
編輯:
df = A B C
1 2 "cheese"
2 4 "spiders"
def samplefunc(item):
print('i hate ' + item)
我想你需要Series.apply
:
def samplefunc(item):
print('i hate ' + item)
df.C.apply(samplefunc)
i hate cheese
i hate spiders
感謝您的編輯。 – jezrael
你可以添加與樣本功能數據的樣本? – jezrael
@jezrael剛剛添加,謝謝! – user5826447