美文网首页
pandas with csv

pandas with csv

作者: Oppenheim | 来源:发表于2017-12-04 14:59 被阅读24次

    import pandas as pd

    pd.read_csv()

    pd.head() 任意读取行数到head里

    pd.loc[]. 取行.  单个数字取单独的行   3:6。取3到5行    2,5,10。 取三行

    注意iloc和loc的区别:loc是按索引取行或者列,可以是int类型,也可以是字符串类型,

    iloc是按行数或者列数取,只能是int类型

    当依照某列重排之后,原始的索引就打乱,可以利用.reset_index(drop=True or False来重新建立索引,True表示丢掉旧的索引建立新的索引,而False表示保留原来的索引的基础上建立新的索引。

    pd中的dataframe可以存储不同的数据类型,不像numpy中只能是单一的数据类型

    取列的时候索引就是标题,记得是已字符串的方式。[]


    Select and display only the columns that use grams for measurement (that end with"(g)"). To accomplish this:

    Use thecolumnsattribute to return the column names infood_infoand convert to a list by calling the methodtolist()

    Create a new list,gram_columns, containing only the column names that end in"(g)". The string methodendswith()returnsTrueif the string object calling the method ends with the string passed into the parentheses.

    Passgram_columnsinto bracket notation to select just those columns and assign the resulting dataframe togram_df

    Then use the dataframe methodhead()to display the first 3 rows ofgram_df.


    column_names_list=food_info.columns.tolist()

    gram_columns=[]

    for c in column_names_list:

    if c.endswith("(g)"):

    gram_columns.append(c)

    gram_df=food_info[gram_columns]

    print(gram_df.head(3))


    pd.to_datetime()函数 将字符串转为datetiem格式

    pd.sort_values("cloumns_name",inplace=True or False,ascending=True or False)

    列的名字   是否生成新的dataframe。按升序还是降序排列

    pd.isnull()判断是否为空  空值返回True

    要取不为空的参照如下代码。

    0 for rows.   1 for columns lambda 快速定义函数并计算表达式

    相关文章

      网友评论

          本文标题:pandas with csv

          本文链接:https://www.haomeiwen.com/subject/xofxixtx.html