Python 记录
1、列表操作
1)列表转字符串
list1 = ["Department of Biology"]
str1 = "".join(list1)
print(str1)
>>> Department of Biology
2)从列表中删除元素
要删除的元素的类型必须与列表中的元素的类型相同
list1 = ["Department of Biology","Brandeis University"]
list1.remove("Department of Biology")
list1
>>> ['Brandeis University']
2、pandas 设置
设置显示行数,若设置为None,则表示不限制
pd.set_option('display.max_rows', 200)
3、pandas 操作
1)修改一行数据
df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=["nihao","zhangsan","nianling"])
df
>>>
nihao zhangsan nianling
0 1 2 3
1 4 5 6
2 7 8 9
修改第一行的数据
df.loc[0] = (5,6,7)
df
>>>
nihao zhangsan nianling
0 5 6 7
1 4 5 6
2 7 8 9
2)将具有多重索引的DataFrame转换为单层索引
df.reset_index()
3)merge 合并
df1 = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=["nihao","zhangsan","nianling"])
df2 = pd.DataFrame([[10,14,45],[8,14,9],[56,5,7]],columns=["nihao","zhangsan","nianling"])
pd.merge(df1,df2,how="outer")
>>> nihao zhangsan nianling
0 1 2 3
1 4 5 6
2 7 8 9
3 10 14 45
4 8 14 9
5 56 5 7
4、笛卡尔积
1)生成笛卡尔积
import itertools
a = itertools.product(range(14,32),range(0,12),range(0,60)) # 生成笛卡尔积
2)将笛卡尔积转换为DataFrame
pd.DataFrame(a,columns=["day","hour","minute"])