解析数据并将这些分组 header。 Python3、pandas
Parse the datas and make those group header. Python3, pandas
我正在解析 .csv file(您可以在此处查看示例文件。)我正在提取第 2 行和第 7 行的数据。没问题。我就是这样做的。
import pandas as pd
import numpy as np
df = pd.read_csv("datas.csv", index_col=0, header=None)
d = {'YSS':'Yahoo!リスティング 12月分 12/1〜12/31',
'YDNRT':'Yahoo!リマーケティング 12月分 12/1〜12/31',
'YDN':' Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31',
'GSN':'Googleリスティング 12月分 12/1〜12/31',
'GDNRM':'Googleリマーケティング 12月分 12/1〜12/31',
'GDN':'Googleディスプレイネットワーク 12月分 12/1〜12/31'}
pat = r'({})'.format('|'.join(d.keys()))
df.loc['アカウント名'] = df.loc['アカウント名'].str.extract(pat, expand=False).dropna().map(d)
df.loc['利用額(Fee抜き)'] = df.loc['利用額(Fee抜き)'].astype(str).apply(lambda x: x.split(".")[0])
df1 = df.loc[['アカウント名', '利用額(Fee抜き)']]
df1 = df1.T
df1.columns = ['項目','金額']
df1['数量'] = 1
df1['単位'] = "式"
df1['単価'] = np.nan
wow = df1[['項目','数量','単位','単価', '金額']]
newFile = wow.shift(1)
newFile['項目'] = newFile['項目'].fillna(df.loc['クライアント名'])
newFile.loc[newFile['項目'].str.contains('プレサンス'),['数量','単位','単価', '金額']] = ['','','','']
pos = newFile.index[newFile['項目'].str.contains('プレサンス')]
d = {}
i = 0
for p in pos:
if p == pos[0]:
d[p] = newFile.loc[:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
elif (i + 1) > len(pos) - 1:
d[p] = newFile.loc[pos[i-1]+1:]
else:
d[p] = newFile.loc[p:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
i = i + 1
pd.concat(d, ignore_index=True)
p.to_csv('newfile.csv', index=False)
正在创建包含新列的新 .csv 文件。你可以在这里看到它。 https://imgur.com/a/5w63Yht但是我还需要做一件事
原来file's row 1 has company names. I want to parse those company names and put those on head of each group like this in the image: https://imgur.com/a/4T2WxYt也需要删除总和...
我不太确定是否可能...
您可以通过为原始 df 编制索引并调用 fillna
, and then filter the lines containing the string 'プレサンス'
and overwrite the row values with a list of empty strings, firstly we shift
行向下 1 来替换列 '項目'
的 NaN
,这样就可以使 header:
In[111]:
newFile = df1.shift(1)
newFile['項目'] = newFile['項目'].fillna(df.loc['クライアント名'])
newFile.loc[newFile['項目'].str.contains('プレサンス'),['数量','単位','単価', '金額']] = ['','','','']
newFile
Out[111]:
項目 金額 数量 単位 単価
1 プレサンス ロジェ 和泉中央
2 Yahoo!リスティング 12月分 12/1〜12/31 YSS 91188 1 式 NaN
3 Yahoo!リマーケティング 12月分 12/1〜12/31 25649 1 式 NaN
4 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 13211 1 式 NaN
5 Googleリスティング 12月分 12/1〜12/31 131742 1 式 NaN
6 Googleリマーケティング 12月分 12/1〜12/31 35479 1 式 NaN
7 Googleディスプレイネットワーク 12月分 12/1〜12/31 18999 1 式 NaN
8 プレサンス グラン 茨木
9 Yahoo!リスティング 12月分 12/1〜12/31 YSS 113373 1 式 NaN
10 Yahoo!リマーケティング 12月分 12/1〜12/31 28775 1 式 NaN
11 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 19010 1 式 NaN
12 Googleリスティング 12月分 12/1〜12/31 158389 1 式 NaN
13 Googleリマーケティング 12月分 12/1〜12/31 45530 1 式 NaN
14 Googleディスプレイネットワーク 12月分 12/1〜12/31 23224 1 式 NaN
15 プレサンス ロジェ 江坂
现在你想添加填充以使其更具可读性,我们可以存储总计所在的索引位置,然后迭代这些并切片 df,将它们添加到字典中,然后调用 concat
垂直堆叠填充的切片:
In[112]:
pos = newFile.index[newFile['項目'].str.contains('プレサンス')]
pos
Out[112]: Int64Index([1, 8, 15], dtype='int64')
现在为每个切片创建一个字典并附加一个空行:
In[115]:
d = {}
i = 0
for p in pos:
if p == pos[0]:
d[p] = newFile.loc[:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
elif (i + 1) > len(pos) - 1:
d[p] = newFile.loc[pos[i-1]+1:]
else:
d[p] = newFile.loc[p:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
i = i + 1
pd.concat(d, ignore_index=True)
Out[115]:
項目 金額 数量 単位 単価
0 プレサンス ロジェ 和泉中央
1 Yahoo!リスティング 12月分 12/1〜12/31 YSS 91188 1 式 NaN
2 Yahoo!リマーケティング 12月分 12/1〜12/31 25649 1 式 NaN
3 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 13211 1 式 NaN
4 Googleリスティング 12月分 12/1〜12/31 131742 1 式 NaN
5 Googleリマーケティング 12月分 12/1〜12/31 35479 1 式 NaN
6 Googleディスプレイネットワーク 12月分 12/1〜12/31 18999 1 式 NaN
7
8 プレサンス グラン 茨木
9 Yahoo!リスティング 12月分 12/1〜12/31 YSS 113373 1 式 NaN
10 Yahoo!リマーケティング 12月分 12/1〜12/31 28775 1 式 NaN
11 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 19010 1 式 NaN
12 Googleリスティング 12月分 12/1〜12/31 158389 1 式 NaN
13 Googleリマーケティング 12月分 12/1〜12/31 45530 1 式 NaN
14 Googleディスプレイネットワーク 12月分 12/1〜12/31 23224 1 式 NaN
15
16 Yahoo!リスティング 12月分 12/1〜12/31 YSS 113373 1 式 NaN
17 Yahoo!リマーケティング 12月分 12/1〜12/31 28775 1 式 NaN
18 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 19010 1 式 NaN
19 Googleリスティング 12月分 12/1〜12/31 158389 1 式 NaN
20 Googleリマーケティング 12月分 12/1〜12/31 45530 1 式 NaN
21 Googleディスプレイネットワーク 12月分 12/1〜12/31 23224 1 式 NaN
22 プレサンス ロジェ 江坂
我正在解析 .csv file(您可以在此处查看示例文件。)我正在提取第 2 行和第 7 行的数据。没问题。我就是这样做的。
import pandas as pd
import numpy as np
df = pd.read_csv("datas.csv", index_col=0, header=None)
d = {'YSS':'Yahoo!リスティング 12月分 12/1〜12/31',
'YDNRT':'Yahoo!リマーケティング 12月分 12/1〜12/31',
'YDN':' Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31',
'GSN':'Googleリスティング 12月分 12/1〜12/31',
'GDNRM':'Googleリマーケティング 12月分 12/1〜12/31',
'GDN':'Googleディスプレイネットワーク 12月分 12/1〜12/31'}
pat = r'({})'.format('|'.join(d.keys()))
df.loc['アカウント名'] = df.loc['アカウント名'].str.extract(pat, expand=False).dropna().map(d)
df.loc['利用額(Fee抜き)'] = df.loc['利用額(Fee抜き)'].astype(str).apply(lambda x: x.split(".")[0])
df1 = df.loc[['アカウント名', '利用額(Fee抜き)']]
df1 = df1.T
df1.columns = ['項目','金額']
df1['数量'] = 1
df1['単位'] = "式"
df1['単価'] = np.nan
wow = df1[['項目','数量','単位','単価', '金額']]
newFile = wow.shift(1)
newFile['項目'] = newFile['項目'].fillna(df.loc['クライアント名'])
newFile.loc[newFile['項目'].str.contains('プレサンス'),['数量','単位','単価', '金額']] = ['','','','']
pos = newFile.index[newFile['項目'].str.contains('プレサンス')]
d = {}
i = 0
for p in pos:
if p == pos[0]:
d[p] = newFile.loc[:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
elif (i + 1) > len(pos) - 1:
d[p] = newFile.loc[pos[i-1]+1:]
else:
d[p] = newFile.loc[p:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
i = i + 1
pd.concat(d, ignore_index=True)
p.to_csv('newfile.csv', index=False)
正在创建包含新列的新 .csv 文件。你可以在这里看到它。 https://imgur.com/a/5w63Yht但是我还需要做一件事
原来file's row 1 has company names. I want to parse those company names and put those on head of each group like this in the image: https://imgur.com/a/4T2WxYt也需要删除总和...
我不太确定是否可能...
您可以通过为原始 df 编制索引并调用 fillna
, and then filter the lines containing the string 'プレサンス'
and overwrite the row values with a list of empty strings, firstly we shift
行向下 1 来替换列 '項目'
的 NaN
,这样就可以使 header:
In[111]:
newFile = df1.shift(1)
newFile['項目'] = newFile['項目'].fillna(df.loc['クライアント名'])
newFile.loc[newFile['項目'].str.contains('プレサンス'),['数量','単位','単価', '金額']] = ['','','','']
newFile
Out[111]:
項目 金額 数量 単位 単価
1 プレサンス ロジェ 和泉中央
2 Yahoo!リスティング 12月分 12/1〜12/31 YSS 91188 1 式 NaN
3 Yahoo!リマーケティング 12月分 12/1〜12/31 25649 1 式 NaN
4 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 13211 1 式 NaN
5 Googleリスティング 12月分 12/1〜12/31 131742 1 式 NaN
6 Googleリマーケティング 12月分 12/1〜12/31 35479 1 式 NaN
7 Googleディスプレイネットワーク 12月分 12/1〜12/31 18999 1 式 NaN
8 プレサンス グラン 茨木
9 Yahoo!リスティング 12月分 12/1〜12/31 YSS 113373 1 式 NaN
10 Yahoo!リマーケティング 12月分 12/1〜12/31 28775 1 式 NaN
11 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 19010 1 式 NaN
12 Googleリスティング 12月分 12/1〜12/31 158389 1 式 NaN
13 Googleリマーケティング 12月分 12/1〜12/31 45530 1 式 NaN
14 Googleディスプレイネットワーク 12月分 12/1〜12/31 23224 1 式 NaN
15 プレサンス ロジェ 江坂
现在你想添加填充以使其更具可读性,我们可以存储总计所在的索引位置,然后迭代这些并切片 df,将它们添加到字典中,然后调用 concat
垂直堆叠填充的切片:
In[112]:
pos = newFile.index[newFile['項目'].str.contains('プレサンス')]
pos
Out[112]: Int64Index([1, 8, 15], dtype='int64')
现在为每个切片创建一个字典并附加一个空行:
In[115]:
d = {}
i = 0
for p in pos:
if p == pos[0]:
d[p] = newFile.loc[:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
elif (i + 1) > len(pos) - 1:
d[p] = newFile.loc[pos[i-1]+1:]
else:
d[p] = newFile.loc[p:pos[i+1]-1].append(pd.Series('',newFile.columns), ignore_index=True)
i = i + 1
pd.concat(d, ignore_index=True)
Out[115]:
項目 金額 数量 単位 単価
0 プレサンス ロジェ 和泉中央
1 Yahoo!リスティング 12月分 12/1〜12/31 YSS 91188 1 式 NaN
2 Yahoo!リマーケティング 12月分 12/1〜12/31 25649 1 式 NaN
3 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 13211 1 式 NaN
4 Googleリスティング 12月分 12/1〜12/31 131742 1 式 NaN
5 Googleリマーケティング 12月分 12/1〜12/31 35479 1 式 NaN
6 Googleディスプレイネットワーク 12月分 12/1〜12/31 18999 1 式 NaN
7
8 プレサンス グラン 茨木
9 Yahoo!リスティング 12月分 12/1〜12/31 YSS 113373 1 式 NaN
10 Yahoo!リマーケティング 12月分 12/1〜12/31 28775 1 式 NaN
11 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 19010 1 式 NaN
12 Googleリスティング 12月分 12/1〜12/31 158389 1 式 NaN
13 Googleリマーケティング 12月分 12/1〜12/31 45530 1 式 NaN
14 Googleディスプレイネットワーク 12月分 12/1〜12/31 23224 1 式 NaN
15
16 Yahoo!リスティング 12月分 12/1〜12/31 YSS 113373 1 式 NaN
17 Yahoo!リマーケティング 12月分 12/1〜12/31 28775 1 式 NaN
18 Yahoo!ディスプレイネットワーク 12月分 12/1〜12/31 19010 1 式 NaN
19 Googleリスティング 12月分 12/1〜12/31 158389 1 式 NaN
20 Googleリマーケティング 12月分 12/1〜12/31 45530 1 式 NaN
21 Googleディスプレイネットワーク 12月分 12/1〜12/31 23224 1 式 NaN
22 プレサンス ロジェ 江坂