私はこのようなパンダDataFrameを持っています:
df = pd.DataFrame(['A',1,2,3,'B',4,5,'C',6,7,8,9])
0
0 A
1 1
2 2
3 3
4 B
5 4
6 5
7 C
8 6
9 7
10 8
11 9
文字列と数字の組み合わせです。このDFを次のように2つの列に分割したいと思います。
name value
0 A 1
1 A 2
2 A 3
3 B 4
4 B 5
5 C 6
6 C 7
7 C 8
8 C 9
これを行うための効率的な方法は何ですか?
次を使用できます。
df = pd.DataFrame({0 :['A',1,2,3,'B',4,5,'C',6,7,8,9]})
#check strings
mask = df[0].astype(str).str.isalpha()
#check if mixed values - numeric with strings
#mask = df[0].apply(lambda x: isinstance(x, str))
#create column to first position, create NaNs filled by forward filling
df.insert(0, 'name', df[0].where(mask).ffill())
#remove rows with same values - with names, rename column
df = df[df['name'] != df[0]].rename(columns={0:'value'}).reset_index(drop=True)
print (df)
name value
0 A 1
1 A 2
2 A 3
3 B 4
4 B 5
5 C 6
6 C 7
7 C 8
8 C 9
または:
out = []
acc = None
for x in df[0]:
#check if strings
if isinstance(x, str):
#assign to variable for tuples
acc = x
else:
#append tuple to out
out.append((acc, x))
print (out)
df = pd.DataFrame(out, columns=['name','value'])
print (df)
name value
0 A 1
1 A 2
2 A 3
3 B 4
4 B 5
5 C 6
6 C 7
7 C 8
8 C 9
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加