test_pandas.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Auther :liuyuqi.gov@msn.cn
  5. @Time :2018/7/5 3:08
  6. @File :test_pandas.py
  7. '''
  8. import pandas as pd
  9. def t1():
  10. a = [['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0']]
  11. df = pd.DataFrame(a, columns=list("ABC"))
  12. print(df.dtypes)
  13. print(df)
  14. def t2():
  15. obj = pd.Series(list('cadaabbcc'))
  16. uniques = obj.unique()
  17. print(obj.dtypes)
  18. print(uniques.shape)
  19. def t3():
  20. df = pd.DataFrame()
  21. df2 = pd.read_csv()
  22. df3 = pd.Series()
  23. pd.concat()
  24. pd.to_datetime()
  25. pd.merge()
  26. pd.Timestamp
  27. def t4():
  28. df = pd.DataFrame(columns=list("AB"), data=[[1, 2], [3, 4]])
  29. df["C"] = None
  30. df["C"][1] = 2
  31. print(df)
  32. def t5():
  33. ser1 = pd.Series([1, 2, 3, 4])
  34. ser2 = pd.Series(range(4), index=["a", "b", "c", "d"])
  35. sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
  36. ser3 = pd.Series(sdata)
  37. # print(ser1)
  38. print(ser2)
  39. # 访问Series
  40. ser2["a"]
  41. # 所有索引
  42. ser2.index
  43. # 所有值
  44. ser2.values
  45. def t6():
  46. df = pd.DataFrame([{"A": "11", "B": "12"}, {"A": "111", "B": "121"}, {"A": "1111", "B": "1211"}])
  47. print(df)
  48. print(df.columns.size) # 列数 2
  49. h, l = df.shape
  50. print(h, l)
  51. print(df.iloc[:, 0].size) # 行数 3
  52. print(df.ix[[0]].index.values[0]) # 索引值 0
  53. print(df.ix[[0]].values[0][0]) # 第一行第一列的值 11
  54. print(df.ix[[1]].values[0][1]) # 第二行第二列的值 121
  55. def t7():
  56. '''
  57. 增加一行/一列
  58. :return:
  59. '''
  60. df = pd.DataFrame([{"A": "11", "B": "12"}, {"A": "1111", "B": "1211"}])
  61. # df.insert(value=list([22, 33]))
  62. df = df.append(pd.DataFrame([{"A": "1133", "B": "1332"}]))
  63. print(df)
  64. # 增加一列:
  65. df = pd.DataFrame([{"A": "11", "B": "12"}, {"A": "1111", "B": "1211"}])
  66. df["is"] = False
  67. print(df)
  68. def t8():
  69. # 修改值不能直接引用:df3["mem"][i],而需要df3.loc["mem"][i]
  70. df = pd.DataFrame([{"A": "11", "B": "12"}, {"A": "1111", "B": "1211"}])
  71. df["is"] = False
  72. # df["is"][0] = True
  73. # df.loc[0][2] = True
  74. # df.loc[:, "is"] = True
  75. df.loc[0, "is"] = True
  76. print(df)
  77. # DataFrame循环遍历
  78. def t9():
  79. df = pd.DataFrame({'a': [1, 2, 3], 'b': [3, 4, 5]})
  80. for row in df.itertuples():
  81. print("the index", row.Index)
  82. print("sum of row", row.a + row.b)
  83. t9()
  84. # result = pd.DataFrame(columns=list(["instanceid", "machineid"]), data=list())
  85. # df = pd.DataFrame({'a': list(range(100)), 'b': [random.random() for i in range(100)]})
  86. # index = pd.MultiIndex.from_product([list('abcd'), list(range(25))])
  87. # df.index = index
  88. # print(df.head())
  89. # df.loc[('a', -1), :] = None
  90. # df.tail()
  91. #
  92. # data = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})
  93. # data.index = pd.MultiIndex.from_tuples([('a', 1), ('b', 1), ('c', 1)])
  94. # data
  95. # new_df = df.append(data)
  96. # new_df.tail()