我的数据框中有一列,其字符串值如图1所示。
我想要做的是将所有nan值从0替换为1,其他字段替换为1(无论其他字段是否为字符串和int) 我试过这个
func_lambda = lambda x: 1 if any(dataframe['Colum'].values != 0) else 0
但是t用1替换所有列。
这是我的df.head
datacol.head(20)
Out[77]:
0 nan
1 4500856427
2 4003363
3 nan
4 16-4989
5 nan
6 nan
7 WVE-78686557032
8 nan
9 4501581113
10 D4-SC-0232737-1/G1023716
11 nan
12 nan
13 4502549104
14 nan
15 nan
16 nan
17 IT008297
18 15\036628
19 299011667
Name: Customer_PO_Number, dtype: object
答案 0 :(得分:1)
检查一下:
import pandas as pd
df = pd.DataFrame({"Customer_PO_Number":
['nan','4500856427','4003363','nan','16 - 4989','nan','nan','WVE - 78686557032',
'nan','4501581113','D4 - SC - 0232737 - 1 / G1023716','nan','nan','4502549104',
'nan','nan','nan','IT008297','15\03662','8','299011667']})
df.replace('nan', 0, inplace=True) # for replacing nan to 0
df[df != 0] = 1 # for replacing others to 1
print(df)
它会为您提供如下输出:
Customer_PO_Number
0 0
1 1
2 1
3 0
4 1
5 0
6 0
7 1
8 0
9 1
10 1
11 0
12 0
13 1
14 0
15 0
16 0
17 1
18 1
19 1
20 1
希望它会对你有所帮助! :)
答案 1 :(得分:1)
您可以使用布尔测试并将结果转换为整数:
(df['Customer_PO_Number'] == 'nan').astype(int)
输出:
0 1
1 0
2 0
3 1
4 0
5 1
6 1
7 0
8 1
9 0
10 0
11 1
12 1
13 0
14 1
15 1
16 1
17 0
18 0
19 0
20 0
Name: Customer_PO_Number, dtype: int32
如果' nan'真的是np.nan然后你可以使用isnull
:
df['Customer_PO_Number'].isnull().astype(int)