如何在Pandas数据框中仅投射少量列

时间:2019-04-12 03:00:27

标签: python pandas

我有一个39列的数据框。我需要将类型强制转换为从5(name = 1980)到39(2013)的int列。我该怎么办?

d [1980:2013] .astype(int)dataframe

3 个答案:

答案 0 :(得分:1)

df.loc [:,1980:2013] .astype(int)

答案 1 :(得分:0)

尝试以下代码:

for i in range(5,40):
    d.iloc[i,:]=map(int,d.iloc[i:])

答案 2 :(得分:0)

您可以使用以索引范围开头的嵌套for循环来解决此问题。

for x in df.columns[5:40]:  <--pulls the columns you want to type cast
    for y in df[x]:  <-- pulls the array from said column
        y = int(y)   <--type casts the entire arrary

...repeats for each column inside your range.