python 创建Pandas DataFrames

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 创建Pandas DataFrames相关的知识,希望对你有一定的参考价值。

#Create DataFrame from lists

import pandas as pd
import numpy as np

countries = ['Russian Fed.', 'Norway', 'Canada', 'United States',
                 'Netherlands', 'Germany', 'Switzerland', 'Belarus',
                 'Austria', 'France', 'Poland', 'China', 'Korea', 
                 'Sweden', 'Czech Republic', 'Slovenia', 'Japan',
                 'Finland', 'Great Britain', 'Ukraine', 'Slovakia',
                 'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan']

gold = [13, 11, 10, 9, 8, 8, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
silver = [11, 5, 10, 7, 7, 6, 3, 0, 8, 4, 1, 4, 3, 7, 4, 2, 4, 3, 1, 0, 0, 2, 2, 2, 1, 0]
bronze = [9, 10, 5, 12, 9, 5, 2, 1, 5, 7, 1, 2, 2, 6, 2, 4, 3, 1, 2, 1, 0, 6, 2, 1, 0, 1]

olympic_medal_counts_df = pd.DataFrame({'country_name' : countries, 'gold': gold,'silver': silver, 'bronze': bronze})

#Creating a filtered DataFrame of bronze with at least 1 gold followed by averaging the series
bronze_at_least_one_gold = olympic_medal_counts_df['bronze'][olympic_medal_counts_df['gold']>=1]
avg_bronze_at_least_one_gold = np.mean(bronze_at_least_one_gold)

#Creating DataFrame series of average gold, bronze and silver
avg_medal_count = df[['gold','silver','bronze']].apply(np.mean)
#Create DataFrame from lists

import pandas as pd
import numpy as np

countries = ['Russian Fed.', 'Norway', 'Canada', 'United States',
                 'Netherlands', 'Germany', 'Switzerland', 'Belarus',
                 'Austria', 'France', 'Poland', 'China', 'Korea', 
                 'Sweden', 'Czech Republic', 'Slovenia', 'Japan',
                 'Finland', 'Great Britain', 'Ukraine', 'Slovakia',
                 'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan']

gold = [13, 11, 10, 9, 8, 8, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
silver = [11, 5, 10, 7, 7, 6, 3, 0, 8, 4, 1, 4, 3, 7, 4, 2, 4, 3, 1, 0, 0, 2, 2, 2, 1, 0]
bronze = [9, 10, 5, 12, 9, 5, 2, 1, 5, 7, 1, 2, 2, 6, 2, 4, 3, 1, 2, 1, 0, 6, 2, 1, 0, 1]

df = pd.DataFrame({'country_name' : countries, 'gold': gold,'silver': silver, 'bronze': bronze})

medal_counts = df[['gold', 'silver', 'bronze']]
points = np.dot(medal_counts, [4,2,1])

olympic_points = {'country_name': countries, 'points':points}
olympic_points_df = pd.DataFrame(olympic_points)
#Create DataFrame from Dictionary

import pandas as pd

data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012], 'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],'wins': [11, 8, 10, 15, 11, 6, 10, 4], 'losses': [5, 8, 6, 1, 5, 10, 6, 12]}

football = pd.DataFrame(data)

print (football.dtypes)
print (football.describe())

print (football.iloc[[0]])
print (football.loc[[0]])
print (football[3:5])
print (football[football.wins > 10])
print (football[(football.wins > 10) & (football.team == "Packers")])

以上是关于python 创建Pandas DataFrames的主要内容,如果未能解决你的问题,请参考以下文章

学习 Python 之 Pandas库

Python--Pandas.2(DataFrame的概念和创建,索引,基本操作)

[python][pandas]DataFrame的基本操作

Python Pandas Dataframe 到 CSV [重复]

如何从包含Python3中特定索引和列的列表的dict创建Pandas DataFrame?

100天精通Python(数据分析篇)——第55天:Pandas之DataFrame对象大总结