我在R
中有这个代码a <- c(NA)
a<- matrix(c(a), nrow = 80)
a<-as.data.frame(a)
a[ c(T,F,F,F), ] <- "aaa"
a[ c(F,T,F,F), ] <- "bbb"
a[ c(F,F,T,F), ] <- "ccc"
a[ c(F,F,F,T), ] <- "ddd"
如何在python中将其作为pandas数据框复制?
str(a)
'data.frame':80 obs。 1个变量:
$ V1:chr“aaa”“bbb”“ccc”“ddd”......
答案 0 :(得分:2)
您可以使用np.tile
并构建一个系列。
import pandas as pd
import numpy as np
s = pd.Series(np.tile(['aaa', 'bbb', 'ccc', 'ddd'], 20) )
print(s.shape) # size - 80 rows
(80,)
print(s.head(10)) # shows only the first 10 rows
0 aaa
1 bbb
2 ccc
3 ddd
4 aaa
5 bbb
6 ccc
7 ddd
8 aaa
9 bbb
dtype: object
如果你想要一个数据帧,你可以这样做:
df = pd.DataFrame({'V1' : np.tile(['aaa', 'bbb', 'ccc', 'ddd'], 20)})
print(df.head(10))
V1
0 aaa
1 bbb
2 ccc
3 ddd
4 aaa
5 bbb
6 ccc
7 ddd
8 aaa
9 bbb
处理动态行:
vals = ['aaa', 'bbb', 'ccc', 'ddd', ...]
tiled_vals = np.tile(vals, (80 // len(vals)) + 1 )[:80] # tile and clip to 80
s = pd.Series(tiled_vals)
或者,
df = pd.DataFrame({'V1' : tiled_vals})
答案 1 :(得分:0)
在Python中:
[('SSL routines', 'ssl3_read_bytes', 'tlsv1 alert unknown ca'), ('SSL routines', 'ssl3_read_bytes', 'ssl handshake failure')]
在R
['aaa', 'bbb', 'ccc', 'ddd']*20