我注意到的最大区别是作业。在import random
import pandas as pd
s = "SummerCrime|WinterCrime".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)
df.FallCrime = [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)]
给出:UserWarning: Pandas doesn't allow columns to be created via a new attribute name
以下警告可能与您的问题有关:
^{pr2}$
他们接着说:You can use attribute access to modify an existing element of a Series or column of a
DataFrame, but be careful; if you try to use attribute access to create a new column,
it creates a new attribute rather than a new column.
**In 0.21.0 and later, this will raise a UserWarning**
(所以你可能是在不知不觉中这么做的)