我已将以下CSV文件输入iPython Notebook:
public = pd.read_csv("categories.csv")
public
Run Code Online (Sandbox Code Playgroud)
我还将pandas导入为pd,将numpy导入为np,将matplotlib.pyplot导入为plt.存在以下数据类型(以下是摘要 - 大约有100列)
In [36]: public.dtypes
Out[37]: parks object
playgrounds object
sports object
roading object
resident int64
children int64
Run Code Online (Sandbox Code Playgroud)
我希望将"公园","游乐场","体育"和"漫游"更改为类别(他们在其中有类似的比例反应 - 每列都有不同类型的喜欢的回应(例如,一个人"非常同意","同意"等等,另一个具有"非常重要","重要"等等,其余部分为int64.
我能够创建一个单独的数据框 - public1 - 并使用以下代码将其中一列更改为类别类型:
public1 = {'parks': public.parks}
public1 = public1['parks'].astype('category')
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用此代码一次更改数字时,我没有成功:
public1 = {'parks': public.parks,
'playgrounds': public.parks}
public1 = public1['parks', 'playgrounds'].astype('category')
Run Code Online (Sandbox Code Playgroud)
尽管如此,我不想仅使用类别列创建单独的数据框.我想在原始数据框中更改它们.
我尝试了很多方法来实现这一点,然后在这里尝试了代码:Pandas:更改列的数据类型 ...
public[['parks', 'playgrounds', 'sports', 'roading']] = public[['parks', 'playgrounds', 'sports', 'roading']].astype('category')
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
NotImplementedError: > 1 ndim Categorical are not supported at this time
Run Code Online (Sandbox Code Playgroud)
有没有办法改变"公园","游乐场","体育","漫步"到类别(这样可以分析比特率的反应),留下"常驻"和"儿童"(以及94个其他列是字符串,int +浮动)请原谅?或者,有更好的方法吗?如果有人有任何建议和/或反馈我会非常感激....我慢慢地去秃头撕开我的头发!
提前谢谢了.
编辑添加 …
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) 我尝试delete一些列并在列中转换一些值
df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True)
df2['date'] = df2['date'].map(lambda x: str(x)[1:])
df2['date'] = df2['date'].str.replace(':', ' ', 1)
df2['date'] = pd.to_datetime(df2['date'])
Run Code Online (Sandbox Code Playgroud)
我得到的所有这些字符串
df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True)
C:/Users/????? ???????????/Desktop/projects/youtube_log/filter.py:11: 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
Run Code Online (Sandbox Code Playgroud)
那有什么问题?
假设我有以下pandas DataFrame:
df = pd.DataFrame({
'team': ['Warriors', 'Warriors', 'Warriors', 'Rockets', 'Rockets'],
'player': ['Stephen Curry', 'Klay Thompson', 'Kevin Durant', 'Chris Paul', 'James Harden']})
Run Code Online (Sandbox Code Playgroud)
当我尝试对team列进行分组并执行操作时,我得到一个SettingWithCopyWarning:
for team, team_df in df.groupby(by='team'):
# team_df = team_df.copy() # produces no warning
team_df['rank'] = 10 # produces warning
team_df.loc[:, 'rank'] = 10 # produces warning
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
df_team['rank'] = 10
Run Code Online (Sandbox Code Playgroud)
如果我取消注释生成子DataFrame副本的行,我不会收到错误.这通常是最好的做法,以避免这种警告或我做错了什么? …