Bra*_*ock 3 python pandas seaborn
我对 Python 很陌生,对 SO 也很陌生。
我有一个名为 df 的 pandas 数据框,如下所示:
Text
Date Location
2015-07-08 San Diego, CA 1
2015-07-07 Bellevue, WA 1
Los Angeles, CA 1
New York, NY 1
Los Angeles, CA 1
Unknown 1
Run Code Online (Sandbox Code Playgroud)
我想使用以下方法来旋转数据:
import pandas, numpy as np
df_pivoted = df.pivot_table(df, values=['Text'], index=['Date'],
columns=['Location'],aggfunc=np.sum)
Run Code Online (Sandbox Code Playgroud)
这个想法是生成一个热图,显示“位置”和“日期”的“文本”计数。
我收到错误:
TypeError: pivot_table() got multiple values for keyword argument 'values'
Run Code Online (Sandbox Code Playgroud)
使用简化方法时:
df = df.pivot_table('Date', 'Location', 'Text')
Run Code Online (Sandbox Code Playgroud)
我收到错误:
raise DataError('No numeric types to aggregate')
Run Code Online (Sandbox Code Playgroud)
我正在使用 Python 2.7 和 Pandas 0.16.2
In[2]: df.dtypes
Out[2]:
Date datetime64[ns]
Text object
Location object
dtype: object
Run Code Online (Sandbox Code Playgroud)
有人有主意吗?
import pandas as pd
import numpy as np
# just try to replicate your dataframe
# ==============================================
date = ['2015-07-08', '2015-07-07', '2015-07-07', '2015-07-07', '2015-07-07', '2015-07-07']
location = ['San Diego, CA', 'Bellevue, WA', 'Los Angeles, CA', 'New York, NY', 'Los Angeles, CA', 'Unknown']
text = [1] * 6
df = pd.DataFrame({'Date': date, 'Location': location, 'Text': text})
Out[141]:
Date Location Text
0 2015-07-08 San Diego, CA 1
1 2015-07-07 Bellevue, WA 1
2 2015-07-07 Los Angeles, CA 1
3 2015-07-07 New York, NY 1
4 2015-07-07 Los Angeles, CA 1
5 2015-07-07 Unknown 1
# processing
# ==============================================
pd.pivot_table(df, index='Date', columns='Location', values='Text', aggfunc=np.sum)
Out[142]:
Location Bellevue, WA Los Angeles, CA New York, NY San Diego, CA Unknown
Date
2015-07-07 1 2 1 NaN 1
2015-07-08 NaN NaN NaN 1 NaN
Run Code Online (Sandbox Code Playgroud)