#DataFrame算术:不重叠部分为NaN,重叠部分元素运算' x = DataFrame ( np . arange ( 9 ) . reshape ( 3 , 3 ) , columns = [ 'A' , 'B' , 'C' ] , index = [ 'a' , 'b' , 'c' ] ) y = DataFrame ( np . arange ( 12 ) . reshape ( ( 4 , 3 ) ) , columns = [ 'A' , 'B' , 'C' ] , index = [ 'a' , 'b' , 'c' , 'd' ] ) print ( x ) A B C a 0 1 2 b 3 4 5 c 6 7 8 print ( y ) A B C a 0 1 2 b 3 4 5 c 6 7 8 d 9 10 11 print ( x + y ) A B C a 0.0 2.0 4.0 b 6.0 8.0 10.0 c 12.0 14.0 16.0 d NaN NaN NaN #对x+y的不重叠部分填充,不是对结果NaN填充 print ( x . add ( y , fill_value = 0 ) ) # x不变化 A B C a 0.0 2.0 4.0 b 6.0 8.0 10.0 c 12.0 14.0 16.0 d 9.0 10.0 11.0 #DataFrame与Series运算:每行/列进行运算 frame = DataFrame ( np . arange ( 9 ) . reshape ( ( 3 , 3 ) ) , columns = [ 'A' , 'B' , 'C' ] , index = [ 'a' , 'b' , 'c' ] ) print ( frame ) A B C a 0 1 2 b 3 4 5 c 6 7 8 series = frame . ix [ 0 ] print ( series ) A 0 B 1 C 2 Name: a, dtype: int32 print ( frame - series ) # 默认按行运算 A B C a 0 0 0 b 3 3 3 c 6 6 6 series2 = Series ( range ( 4 ) , index = [ 'A' , 'B' , 'C' , 'D' ] ) print ( frame + series2 ) # 按行运算:缺失列则为NaN A B C D a 0 2 4 NaN b 3 5 7 NaN c 6 8 10 NaN series3 = frame . A print ( series3 ) a 0 b 3 c 6 Name: A, dtype: int32 print ( frame . sub ( series3 , axis = 0 ) ) # 按列运算 ??(frame的行-series3) A B C a 0 1 2 b 0 1 2 c 0 1 2 算术运算和数据对齐from pandas import Indexfrom pandas import Series,DataFrameimport numpy as npimport pandas as pd#DataFrame算术:不重叠部分为NaN,重叠部分元素运算'x=DataFrame(np.arange(9).reshape(3,3), ...
pandas 显示对于字符宽度的控制默认是英文,当DataFrame的列名含有中文时, pandas 就无法准确的控制列宽,从而导致列名和列没有 对齐 ;对此,我们只要修改下 pandas 显示的默认设置即可,将两项设置都设为True即可,具体如下: pd.set_option('display.unicode.ambiguous_as_wide', True) pd.set_option('display.unicode.east_asian_width', True) pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth', 1000) 输出 结果如...
import pandas as pd #这两个参数的默认设置都是False pd.set_option('display.unicode.ambiguous_as_wide', True) pd.set_option('display.unicode.east_asian_width...