如何将一列系列插入 python 中的单个数据框列?

How do I insert a one column Series into a single dataframe column in python?

我从数据框中取出并复制了一列。简单的。我修改了它,现在我需要把它放回去,但我不知道怎么做。我尝试了无数方法,其中 none 行得通。非常感谢任何帮助。

代码如下: [代码]

for col in ["Shares__Basic_"]:
    tmp_col = data[col]
    count = 0
    index_no = data.columns.get_loc(col)
    while 1:
        result = sm.tsa.stattools.adfuller(tmp_col, autolag='AIC')
        pvalue = result[1]
        if pvalue > 0.01:
            tmp_col = tmp_col.diff()
            count = count + 1
            tmp_col = tmp_col.drop(tmp_col.index[0])
            print(col+" diffed")
        elif pvalue < 0.01:
            break
    while count > 0:
        tmp_col = pd.concat([pd.Series([float("nan")]), tmp_col])
        count = count - 1
    del data[col]
    data.insert(index_no, col, value=tmp_col)

[/code]

使用insert:

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
s = pd.Series([5, 6])
df.insert(0, "new", s)
print(df)

试试这个以在现有列上添加列 -

df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6]}) #DUMMY DATASET
print(df)

#>>    A  B
#>> 0  1  4
#>> 1  2  5
#>> 2  3  6

modified_column = df['A']**2

#Adding it back over the existing columns
df['A'] = modified_column
print(df)

#>>    A  B
#>> 0  1  4
#>> 1  4  5
#>> 2  9  6

如果你想将它添加为附加列,那么试试这个 -

#Adding it back as a new column
df['New_A'] = modified_column
print(df)

#>>    A  B  New_A
#>> 0  1  4      1
#>> 1  2  5      4
#>> 2  3  6      9

编辑:ValueError: cannot reindex from a duplicate axis 通常在您有重复的索引值时发生。您可能不小心破坏了 modified_column 的索引。使用原始数据帧的索引重置它。

modified_column.index = df.index