pandas

>>> import pandas as pd
>>> df = pd.DataFrame([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])
>>> columns = ["col1","col2","col3","col4"]
>>> df
   0  1  2  3
0  1  1  1  1
1  2  2  2  2
2  3  3  3  3
3  4  4  4  4
>>> df = pd.DataFrame([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]],columns=["col1","col2","col3","col4"])
>>> df
   col1  col2  col3  col4
0     1     1     1     1
1     2     2     2     2
2     3     3     3     3
3     4     4     4     4
>>> df.mean(axis=1)
0    1.0
1    2.0
2    3.0
3    4.0
dtype: float64
>>> df.drop("col3",axis=1)
   col1  col2  col4
0     1     1     1
1     2     2     2
2     3     3     3
3     4     4     4
>>> df.drop("1",axis=0)
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    df.drop("1",axis=0)
  File "D:\Python\lib\site-packages\pandas\core\generic.py", line 2161, in drop
    new_axis = axis.drop(labels, errors=errors)
  File "D:\Python\lib\site-packages\pandas\core\indexes\base.py", line 3624, in drop
    labels[mask])
ValueError: labels ['1'] not contained in axis
>>> df.drop(1,axis=0)
   col1  col2  col3  col4
0     1     1     1     1
2     3     3     3     3
3     4     4     4     4
>>> df.index()
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    df.index()
TypeError: 'RangeIndex' object is not callable
>>> df.index = range(4)
>>> df.index
RangeIndex(start=0, stop=4, step=1)
>>> df
   col1  col2  col3  col4
0     1     1     1     1
1     2     2     2     2
2     3     3     3     3
3     4     4     4     4
>>> df1 = df[['col3','col4']]
>>> df1
   col3  col4
0     1     1
1     2     2
2     3     3
3     4     4
>>> df1.to_dict(orient='dict')
{'col3': {0: 1, 1: 2, 2: 3, 3: 4}, 'col4': {0: 1, 1: 2, 2: 3, 3: 4}}
>>> df1.to_dict(orient='records')
[{'col3': 1, 'col4': 1}, {'col3': 2, 'col4': 2}, {'col3': 3, 'col4': 3}, {'col3': 4, 'col4': 4}]
>>> df1.to_dict(orient='records')[0]
{'col3': 1, 'col4': 1}
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值