创建具有多个数据框的 pandas 数据框 [英] Create pandas dataframe with multiple dataframes
问题描述
我有一个这样的csv文件:
I've a csv file like this:
Fruit_Type;Fruit_Color;Fruit_Description
Apple;Green,Red,Yellow;Just an apple
Banana;Green,Yellow;Just a Banana
Orange;Red,Yellow;Just an Orange
Grape;;Just a Grape
(注意:单元格内有逗号,颜色类型编号是可变的,最多可以有三种颜色)
( Note: There're commas inside of a cell and the colors type number is variable with a maximum of three different colors )
我想要的结果是:
水果类型;水果颜色;水果描述
Fruit_Type;Fruit_Color;Fruit_Description
Apple;Green;0;0;Just an apple
Apple;0;Red;0;Just an apple
Apple;0;0;Yellow;Just an apple
Banana;Green;0;0;Just a Banana
Banana;0;Red;0;Just a Banana
Banana;0;0;Yellow;Just a Banana
Orange;Green;0;0;Just an Orange
Orange;0;Red;0;Just an Orange
Orange;0;0;Yellow;Just an Orange
Grape;0;0;0;Just a Grape
Grape;0;0;0;Just a Grape
Grape;0;0;0;Just a Grape
我想将数据帧Fruit_Color列分为3列,这些颜色上的0值不存在.
I want to split the dataframe Fruit_Color column into 3 columns with a 0 value on those colors what aren't present.
我试图像这样转换数据框信息数据框以获取包含一些字符串的行:
I've tryed to convert the dataframe info dataframes like this to get the lines what contais some string:
test.py
#load the csv data into dataframe
data = pd.read_csv(open('test.py','rb'),delimiter=';',encoding='utf-8')
#detect the rows where're the color
Green = data.loc[data['Fruit_Color'].str.contains('Green', case=True)]
Red = data.loc[data['Fruit_Color'].str.contains('Red', case=True)]
Yellow = data.loc[data['Fruit_Color'].str.contains('Yellow', case=True)]
有了这些行,我就包含了特定的颜色,但是我不知道如何用这些数据帧制作连接的数据框,我又怎么知道那些行没有像葡萄这样的颜色?
With that i've the rows what contains specific color but i dont know how i can make the joined dataframe with those dataframes and also how can i know those rows what doesn't have any color like the Grape ?
谢谢.
推荐答案
I suggest use str.get_dummies
:
df = df.join(df.pop('Fruit_Color').str.get_dummies(','))
print (df)
Fruit_Type Fruit_Description Green Red Yellow
0 Apple Just an apple 1 1 1
1 Banana Just a Banana 1 0 1
2 Orange Just an Orange 0 1 1
3 Grape Just a Grape 0 0 0
这篇关于创建具有多个数据框的 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!