pandas入门——创建dataframe

创建dataframe

  • 利用集合创建dataframe
d1 = pd.DataFrame(data=list([1, 2, 3]), index=list(["a", "b", "c"]))
print(d1)
  • 利用数组创建dataframe
d2 = pd.DataFrame(data=numpy.array(list([1, 2, 3, 4, 5, 6, 7])))
print(d2)
  • 利用字典创建dataframe
dictionary3 = {"name": "nick", "age": 12, "sex": "male"}
d3 = pd.DataFrame(data=dictionary3, index=list([1, 2, 3]))
print(d3)
  • 利用series创建dataframe
dictionary4 = {"name": "nick", "age": 12, "sex": "male"}
s4 = pd.Series(data=dictionary4)
d4 = pd.DataFrame(data=s4)
print(d4)
  • 利用嵌套字典创建dataframe
dictionary5 = {"A":{"name": "nick", "age": 12, "sex": "male"},"B":{"name": "nick", "age": 12, "sex": "male"}}
s5 = pd.DataFrame(data=dictionary5)
print(s5)
  • 利用嵌套集合创建dataframe
s6 = pd.DataFrame(data=list([[1, 2, 3, 4], ["a", "b", "c", "d"]]))
print(s6)
  • 利用嵌套数组创建dataframe
s7 = pd.DataFrame(data=numpy.array(list([[1, 2, 3, 4], ["a", "b", "c", "d"]])))
print(s7)
  • 利用字典创建dataframe
s8 = pd.DataFrame.from_dict(
    data={"name": "nick", "age": 12, "sex": "male"}, orient="index")

print(s8)

你可能感兴趣的:(数据挖掘之路,pandas)