简体   繁体   English

在Python中将第一列第一行替换为字符串'date'

[英]Replace first column first row to string 'date' in Python

I have csv file and I have insert current date in the first column. 我有csv文件,并且在第一列中插入了当前日期。

My code is here: 我的代码在这里:

import datetime
from datetime import datetime, timedelta
date=datetime.strftime(datetime.now(), '%Y%m%d')

df=pd.read_csv('C:/Paramter_Generation/LTERaw/'+date+'/ENodeBAlgoSwitch_1.csv',header=None,low_memory=False

  df.insert(0,'Date',date)
  df.to_csv('C:/Paramter_Generation/LTERaw/'+date+'/ENodeBAlgoSwitch_1.csv',header=None,index= False)

The dataframe is here: 数据帧在这里:

  1     20180222  ne_name             handover_algo_switch   
  0     20180222      001  1111000000101000000001000000000   
  2     20180222      002  1111000000101000000001000000000   
  3     20180222      003  1111000000101000000001000000000   

Now I want to change the first column first row '20180222' to string 'date'. 现在我想将第一列第一行'20180222'更改为字符串'date'。

I know following code can change the first column first row to the string 'date': 我知道以下代码可以将第一列第一行更改为字符串“ date”:

    df.set_index('Date', inplace=True)
    df.to_csv('C:/Paramter_Generation/LTERaw/'+date+'/'+names+'')

But after execute that, the column ne_name '001','002','003' will be replace to '1','2','3.' 但是执行完之后,列ne_name'001','002','003'将被替换为'1','2','3'。 I don't want to change other colume property. 我不想更改其他colume属性。

Can anyne have idea how to change the first column first row '20180222' to string 'date' and do not change other column property? 任何人都可以知道如何将第一列第一行“ 20180222”更改为字符串“ date”,并且不更改其他列属性吗?

It seems need: 似乎需要:

f = 'C:/Paramter_Generation/LTERaw/'+date+'/ENodeBAlgoSwitch_1.csv'
df = pd.read_csv(f, dtype={'ne_name':str})
df.index = date

df.to_csv(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM