pandas 2024 4月问题
原创
©著作权归作者所有:来自51CTO博客作者基督徒Isaac的原创作品,请联系作者获取转载授权,否则将追究法律责任
import pandas as pd
df = pd.DataFrame(columns=["col1"]) # shape[0] == 0
# df["col2"] = df["col1"].apply(sum) * 1 # ok
# sum(*[1,2]) # TypeError: 'int' object is not iterable
df[["col1"]].apply(lambda row: sum(*row), axis=1) # ok
# 哪个会出错?
import pandas as pd
df = pd.DataFrame()
df["col1"] = ["col1"]*2
df["col2"] = "col2"
df.index = [f"ind{i}" for i in df.index]
for index, data in df.iterrows():
print(index) # label
print(data)
import pandas as pd
df = pd.DataFrame()
df["客户代码"] = ["客户代码1", "客户代码2", "客户代码2", "客户代码3"]
df["公司代码货币价值"] = [1, 2, None, 3]
# None
# 赋值
df["公司代码货币价值"] = df.apply(lambda row: row["公司代码货币价值"] if row["客户代码"] != "客户代码2" else row["公司代码货币价值"] - .1, axis=1)
print(df)