我正在尝试构建一个函数,该函数可以选择数值或对象类型,并将NaN值替换为与dtype相关的值,例如数值为0,对象为“缺失值”。
我的代码和错误是:
test_df.select_dtypes(include=[float,int]).apply(test_df.fillna(0, inplace=True))
TypeError:("'NoneType‘对象不可调用“,’发生在索引字数‘)
test_df.select_dtypes(include=[object]).apply(test_df.fillna('Missing Value', inplace=True))
TypeError:("'NoneType‘对象不可调用“,’发生在索引地址‘)
我不知道如何解决这些错误。另外,如果有人认为我这样做是错误的,很高兴看到另一个解决方案。我需要预先形成这个行动很多,并寻找最有效的方式来完成这项工作。
虽然我读过一些类似的文章,但这些问题似乎非常具体,涉及到人员、特定问题和代码。
任何帮助都是非常感谢的。
示例数据:
data = {'Address': {0: 'https://www.example.com/',
1: 'https://www.example.com/predictions/doncaster-rovers-vs-shrewsbury-predictions-betting-tips-match-previews/',
3: 'https://www.example.com/freetips/free-golf-betting-tips/',
4: 'https://www.example.com/news/',
6: 'https://www.example.com/leagues/sky-bet-championship/',
7: 'https://www.example.com/freetips/f1-betting-tips-predictions/',
8: 'https://www.example.com/bankroll-builder/',
11: 'https://www.example.com/predictions/hartlepool-vs-eastleigh-predictions-betting-tips-match-previews/',
12: 'https://www.example.com/over-2-5-goals-betting-tips-and-predictions/',
13: 'https://www.example.com/terms-conditions/',
14: 'https://www.example.com/tips/nhl-2019-2020-season-picks-betting-tips/',
15: 'https://www.example.com/news/9-1-win-accumulator-lands-on-monday-night/',
16: 'https://www.example.com/leagues/sky-bet-league-1/',
18: 'https://www.example.com/daily-double-tips/',
19: 'https://www.example.com/leagues/europa-league/',
21: 'https://www.example.com/freetips/free-snooker-betting-tips/',
23: 'https://www.example.com/leagues/uefa-champions-league/',
24: 'https://www.example.com/daily-treble-tips/',
25: 'https://www.example.com/freetips/free-darts-betting-tips/',
26: 'https://www.example.com/app/'},
'Content': {0: 'text/html; charset=UTF-8',
1: 'text/html; charset=UTF-8',
3: 'text/html; charset=UTF-8',
4: 'text/html; charset=UTF-8',
6: 'text/html; charset=UTF-8',
7: 'text/html; charset=UTF-8',
8: 'text/html; charset=UTF-8',
11: 'text/html; charset=UTF-8',
12: 'text/html; charset=UTF-8',
13: 'text/html; charset=UTF-8',
14: 'text/html; charset=UTF-8',
15: 'text/html; charset=UTF-8',
16: 'text/html; charset=UTF-8',
18: 'text/html; charset=UTF-8',
19: 'text/html; charset=UTF-8',
21: 'text/html; charset=UTF-8',
23: 'text/html; charset=UTF-8',
24: 'text/html; charset=UTF-8',
25: 'text/html; charset=UTF-8',
26: 'text/html; charset=UTF-8'},
'Title 1': {0: 'Free Sports Betting Tips From Professional Betting Tipsters',
1: 'Doncaster Rovers vs Shrewsbury Predictions & Tips',
3: 'Free Golf Betting Tips, Predictions & Golf Betting Odds',
4: 'Football Betting Blog | Free Super Tips',
6: 'Championship Predictions & Championship Betting Tips',
7: 'F1 Racing Betting Tips and Predictions | Free Super Tips',
8: 'Betting Bankroll Builder | Free Football Betting Tips | Free Super Tips',
11: 'Hartlepool vs Eastleigh Predictions & Tips',
12: 'Over 2.5 Goals Tips & Over 1.5 Goals Betting Predictions',
13: 'Terms & Conditions | Free Super Tips',
14: 'NHL 2019-2020 Season Picks & Betting Tips',
15: '9/1 Win Accumulator lands on Monday night!',
16: 'League One Predictions & League One Betting Tips | Football Betting',
18: 'Daily Double Tips | Free Super Tips',
19: 'Europa League Predictions & Europa League Betting Tips',
21: 0,
23: 'Champions League Predictions & Champions League Betting Tips',
24: 'Daily Treble Tips | Free Football Betting Tips | Free Super Tips',
25: 'Free Darts Betting Tips and Predictions | Free Super Tips',
26: 'FreeSuperTips Mobile App for iPhone and Android | Download Now!'},
'H1-1': {0: 'The Home of Free Sports Betting Tips',
1: 'Doncaster vs Shrewsbury Predictions',
3: 'Latest Golf Betting Tips',
4: 'News Articles',
6: 'Championship Predictions & Championship Betting Tips',
7: 'Latest F1 Betting Tips & Predictions',
8: 'Bankroll Builder',
11: 'Hartlepool vs Eastleigh Predictions',
12: 'Over 2.5 Goals Betting Tips and Predictions',
13: 'Terms & Conditions',
14: 'NHL 2019-2020 Season Picks & Betting Tips',
15: '9/1 Win Accumulator lands on Monday night!',
16: 'League One Predictions & League One Betting Tips',
18: 'Daily Double Tips',
19: 'Europa League Predictions & Europa League Betting Tips',
21: 0,
23: 'Champions League Predictions & Champions League Betting Tips',
24: 'Daily Treble',
25: 'Latest Darts Tips',
26: 'Download The App'},
'H1-2': {0: 'The Home of Free Sports Betting Tips',
1: 0,
3: 'Free Golf Betting Tips',
4: 0,
6: 0,
7: 'Free F1 Betting Tips',
8: 'Bankroll Builder',
11: 0,
12: 'Free Over 2.5 Goals Tips',
13: 0,
14: 0,
15: 0,
16: 0,
18: 'What is a Daily Double?',
19: 0,
21: 0,
23: 0,
24: 'Free Daily Treble Tips',
25: 'Free Darts Betting Tips',
26: 0},
'H2-1': {0: 'Welcome to Free Super Tips',
1: 'Predictions & Tips',
3: 'Free Golf Betting Tips',
4: 'Sign Up To Our Email Newsletter',
6: 'Sky Bet Championship Predictions 2019/20',
7: 'F1 Betting Tips & Predictions',
8: 'Bankroll',
11: 'Predictions & Tips',
12: 'How does goal betting work?',
13: 0,
14: 'Boston Bruins to Win the Stanley Cup',
15: 'Sign Up To Our Email Newsletter',
16: 'League 1 Betting Previews',
18: 'Where should I back your daily double tips?',
19: 'UEFA Europa League Predictions 2019/20',
21: 0,
23: 'UEFA Champions League Predictions 2019/20',
24: 'What is a Daily Treble?',
25: 'Free Darts Betting Tips & Predictions',
26: 0},
'H2-2': {0: 'Today’s Free Football Betting Tips',
1: 'Reason For Doncaster vs Shrewsbury Predictions',
3: 'When Are Your Golf Tips Posted?',
4: 0,
6: 'Championship Betting Previews',
7: 'This Weeks Formula 1 Betting Tips',
8: 'Bets',
11: 'Reason For Hartlepool vs Eastleigh Predictions',
12: 'What are the advantages of betting on Over 2.5 Goals tips?',
13: 0,
14: 'Dallas Stars to Win the Stanley Cup',
15: 0,
16: 'When do you post your League 1 Tips?',
18: 'Where can I get a free bet to back your\xa0daily double?',
19: 'Europa League Final Predictions',
21: 0,
23: 'Champions League Final Predictions',
24: 'What are the advantages of a Daily Treble?',
25: 'When Are Your Darts Tips Posted?',
26: 0},
'Word Count': {0: 2194,
1: 2052,
3: 1169,
4: 1520,
6: 1311,
7: 973,
8: 698,
11: 2019,
12: 1540,
13: 972,
14: 1486,
15: 1720,
16: 1123,
18: 684,
19: 1677,
21: 0,
23: 1699,
24: 818,
25: 1520,
26: 328},
'Text Ratio': {0: 11.52079,
1: 8.852077,
3: 9.677187,
4: 10.943003,
6: 10.753424,
7: 8.455938,
8: 5.3081274,
11: 9.528880000000001,
12: 11.084525,
13: 10.7027025,
14: 10.938739,
15: 12.078298,
16: 8.26281,
18: 5.3075795,
19: 12.4630165,
21: 0.0,
23: 14.116589999999999,
24: 6.197794,
25: 12.040536999999999,
26: 3.7226769999999996},
'Hash': {0: '3ab5ba34a7c581e883016049e28848cb',
1: 'b255533806a97449e445d66690e6e007',
3: '9ff9a83314e16aabf989686db460d014',
4: '8af6446deabe06eada8e6904af182d20',
6: 'e93988549da9e7bd40c5744cdd42e817',
7: 'e193daa50f94d2a94b97f4bdd077a468',
8: '26e4fe9a95e069d5d413e05e24f4af90',
11: 'd7e1b955b137503cdbe3b59c04a95b73',
12: 'abff28a712fae7a29c981d20ade64b3d',
13: 'bf83435ce5015b77dba70e103402f927',
14: '8964c47ec078178b8648f51bd6b1200f',
15: 'de9beacf3baa076b0bafcaf8da8f398c',
16: '56d40290ecfb52421759097f96797fe6',
18: '27d4a921fa5ac189418ce880beabdf2a',
19: '995417328e28075ceb2a1e01fc38198d',
21: 0,
23: '7914bf5e45a5f2731d9b3ad266b38c19',
24: '8c180a043597529daeb8948b8f916c89',
25: '3856cca3e8809531bcf02189fdd1c2d',
26: 'f2e3259f5a4938deca7b37d88fddd6cd'}}
test_df = pd.DataFrame.from_dict(data)发布于 2020-01-08 11:35:16
因此,您必须传递一个可调用的.apply,这将导致错误,但不需要应用。这对你没什么好处。你想就地修改你的数据帧。您可以利用fillna的“fillna”参数
Parameters
----------
value : scalar, dict, Series, or DataFrame
Value to use to fill holes (e.g. 0), alternately a
dict/Series/DataFrame of values specifying which value to use for
each index (for a Series) or column (for a DataFrame). Values not
in the dict/Series/DataFrame will not be filled. This value cannot
be a list.但是你只需要创建字典,你就可以这样做,比如:
int_or_float_cols = {c:0 for c in test_df.head(0).select_dtypes(include=[float,int])}
object_cols = {c:"Missing" for c in test_df.head(0).select_dtypes(include=[object])}
test_df.fillna(value={**int_or_float_cols,**object_cols}, inplace=True)https://stackoverflow.com/questions/59644221
复制相似问题