【发布时间】:2019-07-10 15:00:15
【问题描述】:
我有 2 个数据帧:longdf 和 shortdf。 Longdf 是“主”列表,我需要基本上匹配从 shortdf 到 longdf 的值,匹配的值替换其他列中的值。 longdf 和 shortdf 都需要大量的数据清理。
目标是达到 df 的“目标”。我试图在我想要的地方使用 for 循环 1)提取 df 单元格中的所有数字,以及 2)从单元格中去除空白/单元格空间。第一:为什么这个 for 循环不起作用?第二:有没有更好的方法来做到这一点?
import pandas as pd
a = pd.Series(['EY', 'BAIN', 'KPMG', 'EY'])
b = pd.Series([' 10wow this is terrible data8 ', '10/ USED TO BE ANOTHER NUMBER/ 2', ' OMG 106 OMG ', ' 10?7'])
y = pd.Series(['BAIN', 'KPMG', 'EY', 'EY' ])
z = pd.Series([108, 102, 106, 107 ])
goal = pd.DataFrame
shortdf = pd.DataFrame({'consultant': a, 'invoice_number':b})
longdf = shortdf.copy(deep=True)
goal = pd.DataFrame({'consultant': y, 'invoice_number':z})
shortinvoice = shortdf['invoice_number']
longinvoice = longdf['invoice_number']
frames = [shortinvoice, longinvoice]
new_list=[]
for eachitemer in frames:
eachitemer.str.extract('(\d+)').astype(float) #extracing all numbers in the df cell
eachitemer.str.strip() #strip the blank/whitespaces in between the numbers
new_list.append(eachitemer)
new_short_df = new_list[0]
new_long_df = new_list[1]
【问题讨论】:
-
我很困惑,为什么你的
shortdf和longdf完全一样? -
好问题:我开始在这里提出一个更长的问题,但将其分解,并且从未更改过变量名。