pandas的datafram的一些使用场景

I/O

Input

从本地读取csv文件的方法pd.read_csv(...)
官方文档 pandas.read_csv
其中常见的参数

ParametersMeaningUsage
sep用来标记界定列的符号(Delimiter to use)默认 ,另外一个参数delimiter用法和功能一致
header指定某一行用作列名。默认的行动是"infer the column". 如果没有使用names,则默认等同于header=0。如果使用了names,则等同于header=None
index_col指定用作索引的列,输入可以是int, str, sequence of int/str, or False,当为False时,强制pandas不使用第一列作为index
quoting指定pandas对双引号的处理,输入 int or QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3)quoting=3quoting=QUOTE_NONE (3) 时,表示不处理双引号中的内容,读取全部数据

列表插入

要求

给定list Alist B 且A和B元素一一对应,根据以上数据创建二维表

实现代码


1 查询

1.1 Scenario
  1. 找到column A中的元素 alpha 所对应的行在column B的元素
Code
# hiden datafram initialize code
>>> df
   Tags     I      want   to      race
0    VB  0.00  0.009300  0.00  0.00012
1    TO  0.00  0.000000  0.99  0.00000
2    NN  0.00  0.000054  0.00  0.00057
3  PPSS  0.37  0.000000  0.00  0.00000

# To find the entry when 'want' is 'NN'
>>> df.loc[df['Tags']=='NN'][' want']
Out:
2    0.000054
Name:  want, dtype: float64

另一种写法

# Initialization
>>> display(data)
      word1        word2  SimLex999  w2v_sim
0       old          new       1.58      0.0
1     smart  intelligent       9.20      0.0
2      hard    difficult       8.77      0.0
3     happy     cheerful       9.55      0.0
4      hard         easy       0.95      0.0

>>> w1_happy = data.word1 == 'happy'
>>> display(w1_happy)
0      False
1      False
2      False
3       True
4      False

>>> data.loc[w1_happy][data.word2 == 'cheerful'].iloc[0] # select the first element of subset
word1           happy
word2        cheerful
SimLex999        9.55
w2v_sim             0
1.2 条件查询
  1. 查询一行中单元格满足条件C的列
code
>>> df
    A   C   D   E
0  11  78   5  11
1  12  98   7  34
2  13  11  11  56
3  89  12  12  78

# Select columns which contains any value between 30 to 40
>>> filter = ((df>=30) & (df<=40)).any()
>>> sub_df = df.loc[: , filter]
>>> print(sub_df)

    B   E
0  34  11
1  31  34
2  16  56
3  41  78

((df>=30) & (df<=40)).any() : (df == 11) returns a same sized dataframe containing only bool values. In this bool dataframe, a cell contains True if the corresponding cell in the original dataframe is 11, otherwise it contains False. Then call any() function on this Boolean dataframe object. It looks over the column axis and returns a bool series. Each value in the bool series represents a column and if value is True then it means that column has one or more 11s.
Then we passed that bool sequence to column section of loc[] to select columns with value 11.

  1. 查询一列中单元格满足条件C的行
code

2 赋值

Scenario

A列,b行的数据赋值

Code
语句

df.loc[index,colum]=values

应用
# hiden datafram initialize code
>>> df
   Tags     I      want   to      race
0    VB  0.00  0.009300  0.00  0.00012
1    TO  0.00  0.000000  0.99  0.00000
2    NN  0.00  0.000054  0.00  0.00057
3  PPSS  0.37  0.000000  0.00  0.00000

>>> df.loc[1,'I']=0.0002

3 查询

3.1 Scenario

查找某一列数据符合条件的行的数量

3.2 Code
语句
  • Solution 1
    value_counts(values,sort=True, ascending=False, normalize=False,bins=None,dropna=True)

  • Solution 2
    dataset.groupby('stance').count()['title']

Parameters:
sort=True: 是否要进行排序;默认进行排序
ascending=False: 默认降序排列;
normalize=False: 是否要对计算结果进行标准化并显示标准化后的结果,默认是False。
bins=None: 可以自定义分组区间,默认是否;
dropna=True:是否删除缺失值nan,默认删除

应用


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值