python mulit函数_python – 将函数应用于MultiIndex pandas.DataFrame列
我有一個MultiIndex pandas DataFrame,我想在其中的一個列中應用一個函數,并將結果分配給同一列.
In [1]:
import numpy as np
import pandas as pd
cols = ['One', 'Two', 'Three', 'Four', 'Five']
df = pd.DataFrame(np.array(list('ABCDEFGHIJKLMNO'), dtype='object').reshape(3,5), index = list('ABC'), columns=cols)
df.to_hdf('/tmp/test.h5', 'df')
df = pd.read_hdf('/tmp/test.h5', 'df')
df
Out[1]:
One Two Three Four Five
A A B C D E
B F G H I J
C K L M N O
3 rows × 5 columns
In [2]:
df.columns = pd.MultiIndex.from_arrays([list('UUULL'), ['One', 'Two', 'Three', 'Four', 'Five']])
df['L']['Five'] = df['L']['Five'].apply(lambda x: x.lower())
df
-c:2: 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
Out[2]:
U L
One Two Three Four Five
A A B C D E
B F G H I J
C K L M N O
3 rows × 5 columns
In [3]:
df.columns = ['One', 'Two', 'Three', 'Four', 'Five']
df
Out[3]:
One Two Three Four Five
A A B C D E
B F G H I J
C K L M N O
3 rows × 5 columns
In [4]:
df['Five'] = df['Five'].apply(lambda x: x.upper())
df
Out[4]:
One Two Three Four Five
A A B C D E
B F G H I J
C K L M N O
3 rows × 5 columns
正如您所看到的,該函數未應用于列,我猜是因為我收到此警告:
-c:2: 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
奇怪的是,這個錯誤有時只會發生,我無法理解它何時發生,何時不發生.
我設法應用函數切片數據框與.loc作為建議的警告:
In [5]:
df.columns = pd.MultiIndex.from_arrays([list('UUULL'), ['One', 'Two', 'Three', 'Four', 'Five']])
df.loc[:,('L','Five')] = df.loc[:,('L','Five')].apply(lambda x: x.lower())
df
Out[5]:
U L
One Two Three Four Five
A A B C D e
B F G H I j
C K L M N o
3 rows × 5 columns
但是我想理解為什么這種行為在進行類似dict的切片時會發生(例如df [‘L’] [‘Five’])而不是在使用.loc切片時.
注意:DataFrame來自一個沒有多索引的HDF文件,這可能是奇怪行為的原因?
編輯:我正在使用Pandas v.0.13.1和NumPy v.1.8.0
總結
以上是生活随笔為你收集整理的python mulit函数_python – 将函数应用于MultiIndex pandas.DataFrame列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python进程数上限_python
- 下一篇: 如何利用python3创建数据表_pyt