[英]Pandas dataframe: Can you assign a label for the column names and/or the df values?
When you define a dataframe in pandas in the following manner 当您以以下方式在pandas中定义数据框时
df = pd.DataFrame([['07-Dec-2015', 1,2],
['08-Dec-2015', 3,4],
['09-Dec-2015', 5,6]],
columns=['Date','FR','UK'])
df.set_index('Date')
Out[1]:
FR UK
Date
07-Dec-2015 1 2
08-Dec-2015 3 4
09-Dec-2015 5 6
is there a way to assign a label to the columns (let's say 'Country') and another label for the dataframe values (lets say 'Hits'). 有没有一种方法可以为列分配标签(比如说“国家/地区”),为数据框值分配另一个标签(比如说“命中”)。 I would like to make it look like this:
我想使它看起来像这样:
As a side note: The dataframe in the attached img above has been created as follows: 附带说明:上面随附的img中的数据框已如下创建:
df = pd.DataFrame()
df['Date'] = ['07-Dec-2015','07-Dec-2015','08-Dec-2015','08-Dec-2015','09-Dec-2015','09-Dec-2015']
df['Country'] = ['UK','FR','UK','FR','UK','FR']
df['Hits'] = [2,1,4,3,6,5]
df = df.set_index(['Date','Country'])
df.unstack()
However this is not good enough for my purpose because in my python application the dataframe constructor is getting passed a numpy array and for the index arg a datetime vector, hence broadly speaking it looks like: pd.DataFrame(numpy.ndarray, columns=columnNames, index=DatetimeIndex)
但这对于我的目的来说还不够好,因为在我的python应用程序中,数据帧构造函数正在传递一个numpy数组,而索引arg则传递一个日期时间向量,因此,从广义上讲,它看起来像:
pd.DataFrame(numpy.ndarray, columns=columnNames, index=DatetimeIndex)
Thanks in advance 提前致谢
You could: 你可以:
df = pd.DataFrame(np.random.random((10, 2)), index=pd.DatetimeIndex(start=date(2015,1,1), periods=10, freq='D'))
df.index.name = 'Date'
df.columns = pd.MultiIndex.from_product([['Hits'], ['UK', 'FR']], names=['', 'Country'])
See MultiIndex
docs . 请参阅
MultiIndex
docs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.