如何在PythonDataFrame中编写和循环列上的列表?
如何在PythonDataFrame中的特定列上编写并循环上面的列表,以便得到结果 数据帧: 第N列(例如)如何在PythonDataFrame中编写和循环列上的列表?,python,pandas,dataframe,Python,Pandas,Dataframe,如何在PythonDataFrame中的特定列上编写并循环上面的列表,以便得到结果 数据帧: 第N列(例如) 或者,是否有其他方法可以执行上述转换而无需使用列表?多谢各位 times变量是要重复的次数。请尝试以下操作: a b c d e f g h a b c d e f g h ... (many times) ... a b c d e f g h 或者这个: import
或者,是否有其他方法可以执行上述转换而无需使用
列表
?多谢各位 times变量是要重复的次数。请尝试以下操作:
a
b
c
d
e
f
g
h
a
b
c
d
e
f
g
h
...
(many times)
...
a
b
c
d
e
f
g
h
或者这个:
import pandas as pd
letters = 'abcdefgh'
times = 4
df = pd.DataFrame({'Column Name': [x for x in letters]*times})
print(df)
ascii\u lowercase
返回字母表中的所有字母。您可以使用list*n
重复列表,其中n是它重复的次数。然后将其分配给数据帧的'Column'
,例如:
将熊猫作为pd导入
列表=['a','b','c','d','e','f','g','h']
df=pd.DataFrame()
df['Column']=列表*5
打印(df)
首先不要将列表
用于变量名,因为内置的(python代码字),然后重复使用:
请不要将您的列表命名为list
您可以使用它返回“不能将序列乘以'float'类型的非整数”,因为N列插入了value=np.nan,其中np.nan是'float'而不是'int'。在这种情况下,我想问一下如何将N列中的值更改为int?谢谢。如果数据框列中有np.nan
,请使用df['column'].fillna(0,inplace=True)
将nan
值替换为0
import pandas as pd
letters = 'abcdefgh'
times = 4
df = pd.DataFrame({'Column Name': [x for x in letters]*times})
print(df)
import pandas as pd
from string import ascii_lowercase
times = 4
df = pd.DataFrame({'Column Name': [x for x in ascii_lowercase[:8]]*times})
print(df)
import numpy as np
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
N = 2
df = pd.DataFrame({'col': np.tile(L, N)})
print (df)
col
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 a
9 b
10 c
11 d
12 e
13 f
14 g
15 h
import pandas as pd
import numpy as np
print(pd.DataFrame(columns=['column name'],data= ['1', np.nan, 'c', 'd', 'e', 'f', 'g', 'h']*5))