csv 为什么dataframe.insert命令添加一个表而不是添加一个值?

hpxqektj  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(113)

我想在执行这个查询时插入一个列DBREMOVECMD;

df1ac.insert(0, column = "DBREMOVECMD", value = ("delete analog19_1.tp_table where NE_ID = " + str(df1ac["NE_ID"]) + " ;"))

我希望从这个查询的输出中得到一列。

print(df1ac)
          LASTCOLLPER  LASTCOLLECTIONPERIOD  TimestampY        FARK    NE_ID
    0  19700101000000                     0  1675227525  1675227525  4135862
    1  19700101000000                     0  1675227525  1675227525  4135863
    2  19700101000000                     0  1675227525  1675227525  4135825
    3  19700101000000                     0  1675227525  1675227525  4135824

    df1ac.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 4 entries, 0 to 3
    Data columns (total 5 columns):
     #   Column                Non-Null Count  Dtype
    ---  ------                --------------  -----
     0   LASTCOLLPER           4 non-null      int64
     1   LASTCOLLECTIONPERIOD  4 non-null      int64
     2   TimestampY            4 non-null      int64
     3   FARK                  4 non-null      int64
     4   NE_ID                 4 non-null      int64
    dtypes: int64(5)
    memory usage: 288.0 bytes

    df1ac.insert(0, column = "DBREMOVECMD", value = ("delete analog19_1.tp_table where NE_ID = " + str(df1ac["NE_ID"]) + " ;"))

    print(df1ac)
                                             DBREMOVECMD  ...    NE_ID
    0  delete analog19_1.tp_table where NE_ID = 0    ...  ...  4135862
    1  delete analog19_1.tp_table where NE_ID = 0    ...  ...  4135863
    2  delete analog19_1.tp_table where NE_ID = 0    ...  ...  4135825
    3  delete analog19_1.tp_table where NE_ID = 0    ...  ...  4135824

    [4 rows x 6 columns]

    df1ac.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 4 entries, 0 to 3
    Data columns (total 6 columns):
     #   Column                Non-Null Count  Dtype 
    ---  ------                --------------  ----- 
     0   DBREMOVECMD           4 non-null      object
     1   LASTCOLLPER           4 non-null      int64 
     2   LASTCOLLECTIONPERIOD  4 non-null      int64 
     3   TimestampY            4 non-null      int64 
     4   FARK                  4 non-null      int64 
     5   NE_ID                 4 non-null      int64 
    dtypes: int64(5), object(1)
    memory usage: 320.0+ bytes

    print(df1ac.loc[0])
    DBREMOVECMD             delete analog19_1.tp_table where NE_ID = 0    ...
    LASTCOLLPER                                                19700101000000
    LASTCOLLECTIONPERIOD                                                    0
    TimestampY                                                     1675227525
    FARK                                                           1675227525
    NE_ID                                                             4135862
    Name: 0, dtype: object

    print(df1ac.loc[0].DBREMOVECMD)
    delete analog19_1.tp_table where NE_ID = 0    4135862
    1    4135863
    2    4135825
    3    4135824
    Name: NE_ID, dtype: int64 ;

    print(df1ac.loc[1].DBREMOVECMD)
    delete analog19_1.tp_table where NE_ID = 0    4135862
    1    4135863
    2    4135825
    3    4135824
    Name: NE_ID, dtype: int64 ;
ibps3vxo

ibps3vxo1#

您的问题是使用str(df1ac["NE_ID"]),它将整个NE_ID列转换为字符串,并将value中只有一个值,然后将其插入到dataframe的 every 行中。相反,您应该使用df1ac["NE_ID"].astype(str)来生成一系列值:

df1ac.insert(0, 
    column="DBREMOVECMD",
    value="delete analog19_1.tp_table where NE_ID = " + df1ac["NE_ID"].astype(str) + " ;"
)

with pd.option_context('display.max_colwidth', None):
    print(df1ac['DBREMOVECMD'])

输出:

0    delete analog19_1.tp_table where NE_ID = 4135862 ;
1    delete analog19_1.tp_table where NE_ID = 4135863 ;
2    delete analog19_1.tp_table where NE_ID = 4135825 ;
3    delete analog19_1.tp_table where NE_ID = 4135824 ;

相关问题