简体   繁体   English

如何使用不同的系列在python中制作数据框?

[英]How do I make a dataframe in python, using different series?

I am trying to make a dataframe using two arrays that are listing average values. 我正在尝试使用列出平均值的两个数组制作一个数据框。 The other two sets of values I would like to incorporate are extracted from another dataset. 我要合并的其他两组值是从另一个数据集中提取的。

This is what I have tried: 这是我尝试过的:

column_a = data_set['column_1']
column_b = data_set['column_2']
df = pd.DataFrame([column_a, column_b, average_1, average_2])
print(df)

The output was strangely formatted: 输出格式奇怪:

                                     0            1            2   
column_a                  North Europe  East Europe  West Europe   
average_1                          143      100.755        175.1   
average_2                        297.9       171.21       227.55   
column_b                        79.265      60.8078       76.468

Is this what you're looking for? 这是您要找的东西吗?

import pandas as pd

d = ({
    'column_a' : ['North Europe','East Europe','West Europe'],  
    'average_1' : [143, 100.755, 175.1],  
    'average_2' : [297.9, 171.21, 227.55 ],                   
    'column_b' : [79.265, 60.8078, 76.468],                                                                         
    })

data_set = pd.DataFrame(d)

column_a = data_set['column_a']
column_b = data_set['column_b']
average_1 = data_set['average_1']
average_2 = data_set['average_2']

df = pd.DataFrame([column_a, column_b, average_1, average_2])

df = df.T
print(df)

       column_a column_b average_1 average_2
0  North Europe   79.265       143     297.9
1   East Europe  60.8078   100.755    171.21
2   West Europe   76.468     175.1    227.55

Or you can use pd.concat instead: 或者您可以使用pd.concat代替:

import pandas as pd

d = ({
    'column_a' : ['North Europe','East Europe','West Europe'],                    
    'column_b' : [79.265, 60.8078, 76.468],                                                                         
     })

data_set1 = pd.DataFrame(d)

d = ({  
    'average_1' : [143, 100.755, 175.1],  
    'average_2' : [297.9, 171.21, 227.55],                                                                                          
     })

data_set2 = pd.DataFrame(d)

column_a = data_set1['column_a']
column_b = data_set1['column_b']
average_1 = data_set2['average_1']
average_2 = data_set2['average_2']

df = pd.concat([column_a, column_b, average_1, average_2], axis = 1)

print(df)

       column_a column_b average_1 average_2
0  North Europe   79.265       143     297.9
1   East Europe  60.8078   100.755    171.21
2   West Europe   76.468     175.1    227.55

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

相关问题 如何将熊猫系列变成 dataframe? - How do I make a panda series into a dataframe? 如何根据所述DataFrame中的另一个系列将布尔系列添加到python DataFrame中? - How do I add a boolean series to a python DataFrame based on another series in said DataFrame? Pandas/Python:如何使用函数过滤 Dataframe 或系列? - Pandas/Python: How can I filter a Dataframe or Series by using functions? 如何比较两个不同长度的Python Pandas系列? - How do I compare two Python Pandas Series of different lengths? 如何在Python中处理来自不同时区的一系列时间戳? - How do I handle a series of timestamps from different timezones in Python? 如何将数据框转换为一系列列表? - How do I turn a dataframe into a series of lists? 如何将 Pandas DataFrame 中的退货系列转换为价格系列? - How do I convert return series in Pandas DataFrame to price series? 如果使用python将序列用逗号分隔,如何将序列转换为数据帧? - How to convert series to dataframe if the series is separated by comma using python? 如何使用下面给出的 pandas Z6A8064B5DF4794555570553 python 中的 seaborn 模块制作热图? - How do I make a heatmap with seaborn module in python using the pandas dataframe given below? 如何使用python中的series计算pi的值? - How do I calculate the value of pi using series in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM