对多个数据框执行类似的分析

时间:2019-06-19 08:36:43

标签: python

我正在从多个数据框中读取数据。 由于索引和输入不同,因此我需要重复配对和分析。我需要特定于数据框的输出。这促使我复制粘贴并重复代码。

是否有一种快速的方法可以引用多个数据框进行相同的分析?

DF1= pd.read_csv('DF1 Price.csv')
DF2= pd.read_csv('DF2 Price.csv')
DF3= pd.read_csv('DF3 Price.csv') # These CSV's contain main prices

DF1['ParentPrice']   = FamPrices ['Price1'] # These CSV's contain second prices
DF2['ParentPrice']   = FamPrices ['Price2']
DF3['ParentPrice']   = FamPrices ['Price3']

DF1['Difference'] = DF1['ParentPrice'] - DF1['Price'] # Price difference is the output
DF2['Difference'] = DF2['ParentPrice'] - DF2['Price']
DF3['Difference'] = DF3['ParentPrice'] - DF3['Price']```

2 个答案:

答案 0 :(得分:0)

可以使用{> {3}}(在python> = 3.6中可用)对字符串进行参数化。在f字符串中,可以在字符串内插入变量值的字符串表示形式,如下所示:

>> a=3
>> s=f"{a} is larger than 11"
>> print(s)
3 is larger than 1!

您的代码将变为:

list_of_DF = []
for symbol in ["1", "2", "3"]:
    df = pd.read_csv(f"DF{symbol} Price.csv")
    df['ParentPrice']   = FamPrices [f'Price{symbol}']
    df['Difference'] = df['ParentPrice'] - df['Price'] 
    list_of_DF.append(df)

然后DF1将是list_of_DF [0],依此类推。

正如我提到的,只有在使用python 3.6或更高版本时,此答案才有效。

答案 1 :(得分:0)

对于第三部分,建议创建类似的内容

DFS=[DF1,DF2,DF3]
def create_difference(dataframe):
    dataframe['Difference'] = dataframe['ParentPrice'] - dataframe['Price']
for dataframe in DFS:
    create_difference(dataframe)

第二种方式,除了可能的以外,我可能不会想到超便捷的方式

for i in range len(DFS) :
    DFS[i]['ParentPrice'] = FamPrices [f'Price{i}']