Pandas 如何将数据帧(df)列的每个值的第一位提取到新的数据帧(df1)

Pandas 如何将数据帧(df)列的每个值的第一位提取到新的数据帧(df1),pandas,Pandas,列价格如下的示例数据帧(df): price 0 2500 1 2600 2 5400 3 3250 4 6245 . . . . 如何将df1实现为: price 0 2 1 2 2 5 3 3 4 6 . . 我想将每个数字转换为字符串,并获取每个列的索引(0),但是否有其他方法可以这样做?将值转换为字符串,然后查看LCT第一个值: df['new'] = df['price'].astype(str).str[0].astype(int) print (df)

列价格如下的示例数据帧(df):

 price
0 2500
1 2600
2 5400
3 3250
4 6245
.   .
.   .
如何将df1实现为:

 price
0  2
1  2
2  5
3  3
4  6
.  .

我想将每个数字转换为字符串,并获取每个列的索引(0),但是否有其他方法可以这样做?

将值转换为字符串,然后查看LCT第一个值:

df['new'] = df['price'].astype(str).str[0].astype(int)
print (df)
   price new
0   2500   2
1   2600   2
2   5400   5
3   3250   3
4   6245   6
或使用整数除法:

df['new'] = df['price'] // 1000
或一般而言:

print (df)
    price
0      20
1     260
2       5
3  325000
4    6245


df['new'] = df['price'] // (10 ** (np.log10(df['price'])).astype(int))
print (df)
    price  new
0      20    2
1     260    2
2       5    5
3  325000    3
4    6245    6

将值转换为字符串并查看LCT第一个值:

df['new'] = df['price'].astype(str).str[0].astype(int)
print (df)
   price new
0   2500   2
1   2600   2
2   5400   5
3   3250   3
4   6245   6
或使用整数除法:

df['new'] = df['price'] // 1000
或一般而言:

print (df)
    price
0      20
1     260
2       5
3  325000
4    6245


df['new'] = df['price'] // (10 ** (np.log10(df['price'])).astype(int))
print (df)
    price  new
0      20    2
1     260    2
2       5    5
3  325000    3
4    6245    6

要附加到现有的数据帧,我应该使用axis=1吗?只需
divmod(df.price,1000)[0]
;)我问了一个重复的问题吗?我的问题被拒绝了,我有一个想法,但无法实现,所以我没有写代码,现在我得到了,伙计,谢谢@anky_91它仅适用于1000的倍数,对吗?若要附加到现有数据帧,是否必须使用axis=1?只需
divmod(df.price,1000)[0]
;)我问了一个重复的问题吗?我的问题被拒绝了,我有一个想法,但无法实现,所以我没有写代码,现在我得到了,伙计,谢谢@anky_91它只适用于1000的倍数,对吗?