我有一个pandas df,并想在这些方面完成一些事情(用SQL术语):
SELECT * FROM df WHERE column1 = 'a' OR column2 = 'b' OR column3 = 'c' etc.
Run Code Online (Sandbox Code Playgroud)
现在这适用于一个列/值对:
foo = df.loc[df['column']==value]
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何将其扩展为多个列/值对
df_masked.loc[:, col] = df_masked.groupby([df_masked.index.month, df_masked.index.day])[col].\
transform(lambda y: y.fillna(y.median()))
Run Code Online (Sandbox Code Playgroud)
即使在使用.loc之后,我也会得到这个.错误,我该如何解决?
Anaconda\lib\site-packages\pandas\core\indexing.py:476: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
Run Code Online (Sandbox Code Playgroud) 我有以下数据帧df:
print(df)
Food Taste
0 Apple NaN
1 Banana NaN
2 Candy NaN
3 Milk NaN
4 Bread NaN
5 Strawberry NaN
Run Code Online (Sandbox Code Playgroud)
我试图使用iloc替换行范围内的值:
df.Taste.iloc[0:2] = 'good'
df.Taste.iloc[2:6] = 'bad'
Run Code Online (Sandbox Code Playgroud)
但是它返回了以下SettingWithCopyWarning消息:
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
Run Code Online (Sandbox Code Playgroud)
所以,我找到了这个Stackoverflow页面并尝试了这个:
df.iloc[0:2, 'Taste'] = 'good'
df.iloc[2:6, 'Taste'] = 'bad'
Run Code Online (Sandbox Code Playgroud)
不幸的是,它返回了以下错误:
ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of …Run Code Online (Sandbox Code Playgroud)