In [1]:
import pandas as pd
In [2]:
print(pd.__version__)
In [3]:
df = pd.DataFrame({'i': [0, 10, 200], 'f': [0, 0.9, 0.09],
's_i': ['0', '10', '200'], 's_f': ['0', '0.9', '0.09']})
In [4]:
print(df)
In [5]:
print(df.dtypes)
In [6]:
print(df['i'].astype(str))
In [7]:
print(df['f'].astype(str))
In [8]:
print(df.astype(str))
In [9]:
print(df.astype(str).dtypes)
In [10]:
print(df['i'].astype(float))
In [11]:
print(df['f'].astype(int))
In [12]:
print(df['s_i'].astype(int))
In [13]:
print(df['s_i'].astype(float))
In [14]:
print(df['s_f'].astype(float))
In [15]:
# print(df['s_f'].astype(int))
# ValueError: invalid literal for int() with base 10: '0.1'
In [16]:
print(df['s_f'].astype(float).astype(int))
In [17]:
df['i'] = df['i'].astype(str)
print(df)
In [18]:
df['f_s'] = df['f'].astype(str)
print(df)
In [19]:
print(df.dtypes)
In [20]:
s_int = pd.Series([0xff, 0o77, 0b11])
In [21]:
print(s_int)
In [22]:
print(s_int.map(bin))
In [23]:
print(s_int.map(oct))
In [24]:
print(s_int.map(hex))
In [25]:
print(s_int.map('{:b}'.format))
In [26]:
print(s_int.map('{:#b}'.format))
In [27]:
print(s_int.map('{:#010b}'.format))
In [28]:
df_str = pd.DataFrame({'bin': ['0b01', '0b10', '0b11'],
'oct': ['0o07', '0o70', '0o77'],
'hex': ['0x0f', '0xf0', '0xff'],
'dec': ['1', '10', '11']})
In [29]:
print(df_str)
In [30]:
print(df_str.dtypes)
In [31]:
# print(df_str['bin'].astype(int))
# ValueError: invalid literal for int() with base 10: '0b01'
In [32]:
print(df_str['bin'].map(lambda x: int(x, 2)))
In [33]:
print(df_str['oct'].map(lambda x: int(x, 8)))
In [34]:
print(df_str['hex'].map(lambda x: int(x, 16)))
In [35]:
print(df_str.applymap(lambda x: int(x, 0)))
In [36]:
print(df_str['dec'].map(lambda x: int(x, 2)))
In [37]:
s_str_dec = pd.Series(['01', '10', '11'])
In [38]:
print(s_str_dec)
In [39]:
print(s_str_dec.astype(int))
In [40]:
# print(s_str_dec.map(lambda x: int(x, 0)))
# ValueError: invalid literal for int() with base 0: '01'
In [41]:
print(df_str['oct'].map(lambda x: int(x, 8)).map(hex))
In [42]:
s_str = pd.Series(['0', '10', 'xxx'])
In [43]:
print(s_str)
In [44]:
print(s_str.str.zfill(8))
In [45]:
print(s_str.str.rjust(8))
In [46]:
print(s_str.str.rjust(8, '_'))
In [47]:
print(s_str.str.center(8))
In [48]:
print(s_str.str.center(8, '_'))
In [49]:
print(s_str.str.ljust(8))
In [50]:
print(s_str.str.ljust(8, '_'))
In [51]:
s_num = pd.Series([0, 10, 100])
In [52]:
# print(s_num.str.rjust(8, '_'))
# AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
In [53]:
print(s_num.astype(str).str.rjust(8, '_'))
In [54]:
df = pd.DataFrame({'i': [0, 10, 100],
'f': [0.1234, 1.234, 12.34],
'round': [0.4, 0.5, 0.6]})
In [55]:
print(df)
In [56]:
print(df.dtypes)
In [57]:
print(df['i'].map('{:08}'.format))
In [58]:
print(df['i'].map('{:_<8}'.format))
In [59]:
print(df['i'].map('{:x}'.format))
In [60]:
print(df['i'].map('{:#010b}'.format))
In [61]:
print(df['f'].map('{:.2f}'.format))
In [62]:
print(df['f'].map('{:.2g}'.format))
In [63]:
print(df['f'].map('{:.2e}'.format))
In [64]:
print(df['f'].map('{:.2%}'.format))
In [65]:
print(df['round'].map('{:.0f}'.format))