小编Pra*_*san的帖子

尝试使用 .loc[row_indexer,col_indexer] = value 代替警告,即使在使用正式

这是我的代码中获得 SettingWithCopyWarning 的行之一:

value1['Total Population']=value1['Total Population'].replace(to_replace='*', value=4)
Run Code Online (Sandbox Code Playgroud)

然后我改为:

row_index= value1['Total Population']=='*'
value1.loc[row_index,'Total Population'] = 4
Run Code Online (Sandbox Code Playgroud)

这仍然给出相同的警告。我该如何摆脱它?

另外,我收到了与我使用过的convert_objects(convert_numeric=True)函数相同的警告,有什么办法可以避免这种情况。

 value1['Total Population'] = value1['Total Population'].astype(str).convert_objects(convert_numeric=True)
Run Code Online (Sandbox Code Playgroud)

这是我收到的警告消息:

试图在来自 DataFrame 的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value 代替

请参阅文档中的警告:http : //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

python pandas

12
推荐指数
2
解决办法
2万
查看次数

计算DataFrame Pandas中“时间”行之间的差异

我的DataFrame格式如下:

       TimeWeek   TimeSat  TimeHoli
0      6:40:00   8:00:00   8:00:00
1      6:45:00   8:05:00   8:05:00
2      6:50:00   8:09:00   8:10:00
3      6:55:00   8:11:00   8:14:00
4      6:58:00   8:13:00   8:17:00
5      7:40:00   8:15:00   8:21:00
Run Code Online (Sandbox Code Playgroud)

我需要在TimeWeek,TimeSat和TimeHoli中找到每一行之间的时间差,输出必须为

TimeWeekDiff   TimeSatDiff  TimeHoliDiff
00:05:00          00:05:00       00:05:00
00:05:00          00:04:00       00:05:00
00:05:00          00:02:00       00:04:00  
00:03:00          00:02:00       00:03:00
00:02:00          00:02:00       00:04:00 
Run Code Online (Sandbox Code Playgroud)

我尝试使用(d['TimeWeek']-df['TimeWeek'].shift().fillna(0),它会引发错误:

TypeError: unsupported operand type(s) for -: 'str' and 'str'
Run Code Online (Sandbox Code Playgroud)

可能是因为该列中存在“:”。我该如何解决?

python row pandas difference

4
推荐指数
2
解决办法
5832
查看次数

标签 统计

pandas ×2

python ×2

difference ×1

row ×1