用通过比较另一列的数据获得的数据填充一列
Filling a column with data obtained by comparing data from another column
我有一个table。我正在创建一个新专栏 "Timing"。我需要用数字填充它,具体取决于列 "type" 中的数据。例如,如果 "type" 列的单元格中是 dgv,那么 "timing" 列中应该有一个数字 17,如果是 ds,则为 8,如果是 psp,则为 3,等等。有几个条件。
part of the table
等等
我的代码:
import csv
with open('C:/Notebook/data1.txt','r') as csvinput:
with open('C:/Notebook/datawr1.txt', 'w') as csvoutput:
writer = csv.writer(csvoutput, lineterminator='\n')
reader = csv.reader(csvinput)
all = []
row = next(reader)
row.append('Timing') # Here I create a column "Timing"
all.append(row)
for row in reader: #I think here should be a condition if
row.append(' ')
all.append(row)
writer.writerows(all)
我想你可以通过字典d
使用map
,如果不匹配得到NaN
:
df = pd.DataFrame({'type':['dgv','ds','psp', 'a']})
print (df)
type
0 dgv
1 ds
2 psp
3 a
d = {'dgv':17,'ds':8,'psp':3}
df['Timing'] = df['type'].map(d)
print (df)
type Timing
0 dgv 17.0
1 ds 8.0
2 psp 3.0
3 a NaN
编辑:
在pandas中读取文件是使用read_csv
, for writing to_csv
(如果是.txt文件没问题):
import pandas as pd
from pandas.compat import StringIO
temp=u"""code,type,date,quantity
0,dgv,07.11.2016,1
0,dgv,08.06.2016,1
0,ds,01.07.2016,1
0,ds,03.08.2016,1
0,ds,03.08.2016,1
0,psp,06.03.2016,1
0,a,07.08.2016,1"""
#after testing replace 'StringIO(temp)' to 'filename.txt'
df = pd.read_csv(StringIO(temp))
print (df)
code type date quantity
0 0 dgv 07.11.2016 1
1 0 dgv 08.06.2016 1
2 0 ds 01.07.2016 1
3 0 ds 03.08.2016 1
4 0 ds 03.08.2016 1
5 0 psp 06.03.2016 1
6 0 a 07.08.2016 1
d = {'dgv':17,'ds':8,'psp':3}
df['Timing'] = df['type'].map(d)
print (df)
code type date quantity Timing
0 0 dgv 07.11.2016 1 17.0
1 0 dgv 08.06.2016 1 17.0
2 0 ds 01.07.2016 1 8.0
3 0 ds 03.08.2016 1 8.0
4 0 ds 03.08.2016 1 8.0
5 0 psp 06.03.2016 1 3.0
6 0 a 07.08.2016 1 NaN
df.to_csv('myfile.txt', index=False)
我有一个table。我正在创建一个新专栏 "Timing"。我需要用数字填充它,具体取决于列 "type" 中的数据。例如,如果 "type" 列的单元格中是 dgv,那么 "timing" 列中应该有一个数字 17,如果是 ds,则为 8,如果是 psp,则为 3,等等。有几个条件。
part of the table 等等
我的代码:
import csv
with open('C:/Notebook/data1.txt','r') as csvinput:
with open('C:/Notebook/datawr1.txt', 'w') as csvoutput:
writer = csv.writer(csvoutput, lineterminator='\n')
reader = csv.reader(csvinput)
all = []
row = next(reader)
row.append('Timing') # Here I create a column "Timing"
all.append(row)
for row in reader: #I think here should be a condition if
row.append(' ')
all.append(row)
writer.writerows(all)
我想你可以通过字典d
使用map
,如果不匹配得到NaN
:
df = pd.DataFrame({'type':['dgv','ds','psp', 'a']})
print (df)
type
0 dgv
1 ds
2 psp
3 a
d = {'dgv':17,'ds':8,'psp':3}
df['Timing'] = df['type'].map(d)
print (df)
type Timing
0 dgv 17.0
1 ds 8.0
2 psp 3.0
3 a NaN
编辑:
在pandas中读取文件是使用read_csv
, for writing to_csv
(如果是.txt文件没问题):
import pandas as pd
from pandas.compat import StringIO
temp=u"""code,type,date,quantity
0,dgv,07.11.2016,1
0,dgv,08.06.2016,1
0,ds,01.07.2016,1
0,ds,03.08.2016,1
0,ds,03.08.2016,1
0,psp,06.03.2016,1
0,a,07.08.2016,1"""
#after testing replace 'StringIO(temp)' to 'filename.txt'
df = pd.read_csv(StringIO(temp))
print (df)
code type date quantity
0 0 dgv 07.11.2016 1
1 0 dgv 08.06.2016 1
2 0 ds 01.07.2016 1
3 0 ds 03.08.2016 1
4 0 ds 03.08.2016 1
5 0 psp 06.03.2016 1
6 0 a 07.08.2016 1
d = {'dgv':17,'ds':8,'psp':3}
df['Timing'] = df['type'].map(d)
print (df)
code type date quantity Timing
0 0 dgv 07.11.2016 1 17.0
1 0 dgv 08.06.2016 1 17.0
2 0 ds 01.07.2016 1 8.0
3 0 ds 03.08.2016 1 8.0
4 0 ds 03.08.2016 1 8.0
5 0 psp 06.03.2016 1 3.0
6 0 a 07.08.2016 1 NaN
df.to_csv('myfile.txt', index=False)