我正在学习udacity数据分析课程,我在理解答案时遇到了困难。
已经被要求“为红色数据帧创建颜色阵列”。
答案是
color_red = np.repeat('red', red_df.shape[0])据我所知,在np.repeat中,第一个参数是输入数组"red",第二个参数是对每个元素red_df.shape[0]重复。
如果为np.repeat(3, 4),则返回array([3, 3, 3, 3])。
有谁能帮我找到正确的思路吗?
发布于 2019-02-24 04:17:30
进入与numpy和pandas的交互式Python会话,并进行实验
创建数据帧:
In [394]: df=pd.DataFrame(np.eye(3))
In [395]: df
Out[395]:
0 1 2
0 1.0 0.0 0.0
1 0.0 1.0 0.0
2 0.0 0.0 1.0检查其shape。这是一个tuple (基本Python对象):
In [396]: df.shape
Out[396]: (3, 3)
In [397]: df.shape[0] # first element of the tuple
Out[397]: 3使用shape参数重复操作就像使用数字3一样:
In [398]: np.repeat('red', df.shape[0])
Out[398]: array(['red', 'red', 'red'], dtype='<U3')Pandas和numpy在Python中运行。因此,Python的常规求值顺序适用。
发布于 2021-09-30 23:18:48
这部分颜色只是返回一个整数与red_df中的总行数,以创建新的添加列‘(red_df.shape[0])’与其相关的red_df的原始数量相同的颜色,因此,当我们稍后与white_df追加它时,它不会向下移动其他white_df和创建其他列上的空行。
您可以简单地删除此部分,并将其编写为:
color_red = np.repeat('red', red_df.shape[0])
color_red = np.repeat('red', 1599)完整的程序将是
import pandas as pd
import numpy as np
df_red = pd.read_csv('winequality-red.csv',sep=';')
df_white = pd.read_csv('winequality-white.csv',sep=';')
print(df_red.info())
print(df_red.shape[0])
# shape[0} refer to the number of columns which is 1599 shape[1] refer to the number of rows which is 12
# create color array for red dataframe
color_red = np.repeat('red', 1599)
# create color array for white dataframe
color_white = np.repeat('white', df_white.shape[0])
df_red['color'] = color_red
df_white['color'] = color_white
#combine data frame into one data frame called wine_df
wine_df = df_red.append(df_white)
print(wine_df.head())
wine_df.to_csv('winequality_edited.csv', index=False)https://stackoverflow.com/questions/54845587
复制相似问题