“功能”对象在熊猫中没有属性“ str”

时间:2020-03-11 20:38:49

标签: python pandas dataframe

我正在使用下面的代码读取和拆分用/

分隔的csv文件字符串
DATA IS 
SRC_PATH                        TGT_PATH
/users/sn/Retail                /users/am/am
/users/sn/Retail Reports/abc    /users/am/am
/users/sn/Automation            /users/am/am
/users/sn/Nidh                  /users/am/xzy

将熊猫作为pd导入

df = pd.read_csv('E:\RCTemplate.csv',index_col=None, header=0)
s1 = df.SRC_PATH.str.split('/', expand=True)

我在s1中获得了正确的拆分数据,但是当我要对单行执行类似的操作时,它会抛出错误“函数对象没有属性'str'”

错误抛出以下代码

df2= [(df.SRC_PATH.iloc[0])]
df4=pd.DataFrame([(df.SRC_PATH.iloc[0])],columns = ['first'])
newvar = df4.first.str.split('/', expand=True)

1 个答案:

答案 0 :(得分:3)

Pandas认为您正在尝试访问方法dataframe.first()

这就是为什么最佳实践是使用硬括号访问数据框列而不是Data_Statistics %>% pivot_longer(cols = c(Median, Minimum,Maximum), names_to = "Statistic",values_to = "Value") %>% ggplot(aes(x = NewDate))+ geom_ribbon(aes(ymin = Lower, ymax = Upper, fill = "Upper / Lower"), alpha =0.5)+ geom_line(aes(y = Value, color = Statistic, linetype = Statistic, size = Statistic))+ facet_wrap(~variable, scales = "free")+ scale_x_date(date_labels = "%b", date_breaks = "month", name = "Month")+ ylab("Daily Cumulative Precipitation (mm)")+ scale_size_manual(values = c(1.5,1,1.5))+ scale_linetype_manual(values = c("dashed","solid","dashed"))+ scale_color_manual(values = c("red","darkblue","black"))+ scale_fill_manual(values = "cyan", name = "") 访问

facets而不是.column

但这不会引起常见问题,例如称为df4['first'].str.split()的列最终会成为数据框的df4.first.str.split()属性,并且还会带来许多其他问题