简体   繁体   中英

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

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:

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM