apply(fun(x,y))用法
def t_or_w(x,b_or_t):for a,b in zip(x,b_or_t):
#print(a)
#print(b)
if '千' in str(a):
#print(1)
b=int(b)*1000
#print(b)
return b
elif '万' in str(a):
b=int(b)*10000
#print(b)
return b
df.apply(t_or_w(df['salary'],df['bottom']),axis=1)
怎么把df.apply(t_or_w(df['salary'],df['bottom']),axis=1)的结果更新到列表里,即想要实现df.bottom=df.apply(t_or_w(df['salary'],df['bottom']),axis=1) 先去看看apply的基本用法,基本例子,不要自己瞎写{:10_312:} 试试:
def t_or_w(x, a_name, b_name):
a = x
b = x
if '千' in str(a):
x = int(b)*1000
elif '万' in str(a):
x = int(b)*10000
return x
df.apply(t_or_w, axis=1, args=('salary','bottom'))
会直接改变原来的 df
{:5_108:}
不知道是不是你想要的代碼:
import pandas as pd
import numpy as np
salary =
values = ["千", "萬", "千", "千", "萬"]
df = pd.DataFrame()], columns = ['A', 'B'])
print(df, end = "\n\n")
def funct(arr: list):
a, b = arr
if b == "千":
return a*1000
else:
return a*10000
data = df.apply(funct, axis=1)
print(data) AB
013千
1 5萬
2 8千
311千
4 9萬
0 13000
1 50000
2 8000
3 11000
4 90000
dtype: int64
页:
[1]