zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python操作pandas的笔记

2023-09-14 09:06:34 时间
import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie', 'David'],
        'age': [25, 30, 35, 40],
        'gender': ['F', 'M', 'M', 'M']}
df = pd.DataFrame(data)
print(df)
print("=" * 30)
new_df = pd.DataFrame(df.iloc[1]).T  # 选取第几行
new_row = pd.DataFrame(df.loc[3]).T
new_df.reset_index(drop=True, inplace=True)  # 重置下标为从0开始
new_df = new_df.append(new_row, ignore_index=True)  # ignore_index将索引改为从0开始以此往下,否则保留copy过来数据的原索引

print(new_df)

其他操作

import pyarrow.parquet as pq

"""
横向:axis=1,纵向axis=0
"""
#file = "/Users/chennan/Downloads/1.parquet"
file2 = "/Users/chennan/Downloads/2.parquet"

df = pq.read_pandas(file2).to_pandas()
# print(df.T) #行列倒置
# print(df.columns)  # DataFrame的列索引列表
# print(df.values)  # 直接获取其中array的值
# print(df.index)  # DataFrame的行索引列表,修复index的时候不能单独修改。
# print(df.loc[0].URL)
print(df.head(5))  # 显示前5行内容
# print(df.tail(5)) #显示后5行内容
# 删除一些列,让数据更简单些,再去做后面的操作
# data = df.drop(["WIDTH","HEIGHT","similarity","hash","punsafe","pwatermark","aesthetic"], axis=1)
# df.iloc[0, -1] = b"https://cdn.mumsgrapevine.com.au/site/wp-content/uploads/2020/03/First-Easter-Shoes-360x241.jpg"
# loc后面第一部分是条件判断,逗号后面是选取列

# 使用loc:只能指定行列索引的名字
# print(df.loc[0, "URL"])  # 第0行的URL列。
# print(df.loc[1, "URL"])

# 获取前2行,前3列的内容,
# print(df.iloc[:2, :3])

# 获取前2行,前
# print(df.iloc[:2, 0])
# # df.to_parquet(file)
#
#
#
# print(df.shape) #行,列

# 赋值操作
# print(df["URL"])
# 或者
# print(df.URL)
# 这一列都变成1
# df["URL"]=1
# np.all()只要有一个就返回False
# print(np.all(pd.notnull(df)))  # 判断是否有缺失值
# print(np.any(pd.isnull(df))) # 判断是否有缺失值,判断是否是缺失值,是则返回True。
# print(df.iloc[0, 0])
# print(df.iloc[1, 0])
# df.iloc[0, 0] = b"123"
# df.iloc[1, 0] = b"234"
# df.to_parquet(file2)
#print(df.iloc[:8, 0][2])
#print(df.iloc[:8, 0][3])